Skip to content

Commit b7eae6b

Browse files
author
Tony Sappe
committed
Changed BubbleSort.py and InsertionSort.py to allow x number of inputs. Also worked on LinearSearch.py
1 parent 549915a commit b7eae6b

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

BubbleSort.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ def simple_bubble_sort(int_list):
1212
return int_list
1313

1414

15-
def main(num):
16-
inputs = []
17-
print("Enter any {} numbers for unsorted list: ".format(num))
15+
def main():
1816
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+
2224
except Exception as e:
2325
print(e)
2426
else:
@@ -27,5 +29,4 @@ def main(num):
2729

2830
if __name__ == '__main__':
2931
print('==== Bubble Sort ====\n')
30-
list_count = 6
31-
main(list_count)
32+
main()

InsertionSort.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
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):
45
temp = int_list[i]
56
j = i - 1
67
while(j >= 0 and temp < int_list[j]):
@@ -11,13 +12,15 @@ def simple_insertion_sort(int_list):
1112
return int_list
1213

1314

14-
def main(num):
15-
inputs = []
16-
print('Enter any {} numbers for unsorted list: '.format(num))
15+
def main():
1716
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+
2124
except Exception as e:
2225
print(e)
2326
else:
@@ -26,5 +29,4 @@ def main(num):
2629

2730
if __name__ == '__main__':
2831
print('==== Insertion Sort ====\n')
29-
list_count = 6
30-
main(list_count)
32+
main()

LinearSearch.py

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
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)
211

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('\nEnter 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()

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### **All Algorithms implemented in Python!**
44

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.
56

67
## Sorting
78

0 commit comments

Comments
 (0)