File tree 4 files changed +44
-37
lines changed
4 files changed +44
-37
lines changed Original file line number Diff line number Diff line change @@ -12,13 +12,15 @@ def simple_bubble_sort(int_list):
12
12
return int_list
13
13
14
14
15
- def main (num ):
16
- inputs = []
17
- print ("Enter any {} numbers for unsorted list: " .format (num ))
15
+ def main ():
18
16
try :
19
- for i in range (num ):
20
- n = input ()
21
- inputs .append (n )
17
+ print ("Enter numbers separated by spaces:" )
18
+ s = raw_input ()
19
+ inputs = list (map (int , s .split (' ' )))
20
+ if len (inputs ) < 2 :
21
+ print ('No Enough values to sort!' )
22
+ raise Exception
23
+
22
24
except Exception as e :
23
25
print (e )
24
26
else :
@@ -27,5 +29,4 @@ def main(num):
27
29
28
30
if __name__ == '__main__' :
29
31
print ('==== Bubble Sort ====\n ' )
30
- list_count = 6
31
- main (list_count )
32
+ main ()
Original file line number Diff line number Diff line change 1
1
2
2
def simple_insertion_sort (int_list ):
3
- for i in range (1 , 6 ):
3
+ count = len (int_list )
4
+ for i in range (1 , count ):
4
5
temp = int_list [i ]
5
6
j = i - 1
6
7
while (j >= 0 and temp < int_list [j ]):
@@ -11,13 +12,15 @@ def simple_insertion_sort(int_list):
11
12
return int_list
12
13
13
14
14
- def main (num ):
15
- inputs = []
16
- print ('Enter any {} numbers for unsorted list: ' .format (num ))
15
+ def main ():
17
16
try :
18
- for i in range (num ):
19
- n = input ()
20
- inputs .append (n )
17
+ print ("Enter numbers separated by spaces:" )
18
+ s = raw_input ()
19
+ inputs = list (map (int , s .split (' ' )))
20
+ if len (inputs ) < 2 :
21
+ print ('No Enough values to sort!' )
22
+ raise Exception
23
+
21
24
except Exception as e :
22
25
print (e )
23
26
else :
@@ -26,5 +29,4 @@ def main(num):
26
29
27
30
if __name__ == '__main__' :
28
31
print ('==== Insertion Sort ====\n ' )
29
- list_count = 6
30
- main (list_count )
32
+ main ()
Original file line number Diff line number Diff line change 1
- def sequentialSearch (alist , item ):
2
- pos = 0
3
- found = False
4
-
5
- while pos < len (alist ) and not found :
6
-
7
- if alist [pos ] == item :
8
- found = True
9
- print ("Found" )
10
- else :
11
- pos = pos + 1
12
- if found == False :
13
- print ("Not found" )
14
- return found
15
-
16
- print ("Enter numbers seprated by space" )
17
- s = input ()
18
- numbers = list (map (int , s .split ()))
19
- trgt = int ( input ('enter a single number to be found in the list ' ))
20
- sequentialSearch (numbers , trgt )
21
1
2
+ def sequential_search (alist , target ):
3
+ for index , item in enumerate (alist ):
4
+ if item == target :
5
+ print ("Found target {} at index {}" .format (target , index ))
6
+ break
7
+ else :
8
+ print ("Not found" )
9
+
10
+
11
+ def main ():
12
+ try :
13
+ print ("Enter numbers separated by spaces" )
14
+ s = raw_input ()
15
+ inputs = list (map (int , s .split (' ' )))
16
+ target = int (raw_input ('\n Enter a single number to be found in the list: ' ))
17
+ except Exception as e :
18
+ print (e )
19
+ else :
20
+ sequential_search (inputs , target )
21
+
22
+ if __name__ == '__main__' :
23
+ print ('==== Insertion Sort ====\n ' )
24
+ main ()
Original file line number Diff line number Diff line change 2
2
3
3
### ** All Algorithms implemented in Python!**
4
4
5
+ These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons.
5
6
6
7
## Sorting
7
8
You can’t perform that action at this time.
0 commit comments