Skip to content

Commit 93443cb

Browse files
Merge pull request #7 from santhon/improvements
Improvements
2 parents 5378088 + f9af3e8 commit 93443cb

File tree

6 files changed

+278
-119
lines changed

6 files changed

+278
-119
lines changed

BubbleSort.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
2-
3-
array=[];
4-
5-
# input
6-
print ("Enter any 6 Numbers for Unsorted Array : ");
7-
for i in range(0, 6):
8-
n=input();
9-
array.append(int(n));
10-
11-
# Sorting
12-
print("")
13-
for i in range(0, 6):
14-
for j in range(0,5):
15-
if (array[j]>array[j+1]):
16-
temp=array[j];
17-
array[j]=array[j+1];
18-
array[j+1]=temp;
19-
20-
# Output
21-
for i in range(0,6):
22-
print(array[i]);
23-
24-
25-
26-
1+
import sys
2+
3+
4+
def simple_bubble_sort(int_list):
5+
count = len(int_list)
6+
swapped = True
7+
while (swapped):
8+
swapped = False
9+
for j in range(count - 1):
10+
if (int_list[j] > int_list[j + 1]):
11+
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
12+
swapped = True
13+
return int_list
14+
15+
16+
def main():
17+
# Python 2's `raw_input` has been renamed to `input` in Python 3
18+
if sys.version_info.major < 3:
19+
input_function = raw_input
20+
else:
21+
input_function = input
22+
23+
try:
24+
print("Enter numbers separated by spaces:")
25+
s = input_function()
26+
inputs = list(map(int, s.split(' ')))
27+
if len(inputs) < 2:
28+
print('No Enough values to sort!')
29+
raise Exception
30+
31+
except Exception as e:
32+
print(e)
33+
else:
34+
sorted_input = simple_bubble_sort(inputs)
35+
print('\nSorted list (min to max): {}'.format(sorted_input))
36+
37+
if __name__ == '__main__':
38+
print('==== Bubble Sort ====\n')
39+
main()

InsertionSort.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1-
array=[];
1+
import sys
22

3-
# input
4-
print ("Enter any 6 Numbers for Unsorted Array : ");
5-
for i in range(0, 6):
6-
n=input();
7-
array.append(int(n));
83

9-
# Sorting
10-
print("")
11-
for i in range(1, 6):
12-
temp=array[i]
13-
j=i-1;
14-
while(j>=0 and temp<array[j]):
15-
array[j+1]=array[j];
16-
j-=1;
17-
array[j+1]=temp;
4+
def simple_insertion_sort(int_list):
5+
count = len(int_list)
6+
for i in range(1, count):
7+
temp = int_list[i]
8+
j = i - 1
9+
while(j >= 0 and temp < int_list[j]):
10+
int_list[j + 1] = int_list[j]
11+
j -= 1
12+
int_list[j + 1] = temp
1813

19-
# Output
20-
for i in range(0,6):
21-
print(array[i]);
14+
return int_list
2215

2316

17+
def main():
18+
# Python 2's `raw_input` has been renamed to `input` in Python 3
19+
if sys.version_info.major < 3:
20+
input_function = raw_input
21+
else:
22+
input_function = input
2423

24+
try:
25+
print("Enter numbers separated by spaces:")
26+
s = input_function()
27+
inputs = list(map(int, s.split(' ')))
28+
if len(inputs) < 2:
29+
print('No Enough values to sort!')
30+
raise Exception
2531

32+
except Exception as e:
33+
print(e)
34+
else:
35+
sorted_input = simple_insertion_sort(inputs)
36+
print('\nSorted list (min to max): {}'.format(sorted_input))
37+
38+
if __name__ == '__main__':
39+
print('==== Insertion Sort ====\n')
40+
main()

LinearSearch.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
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)
1+
import sys
212

3+
4+
def sequential_search(alist, target):
5+
for index, item in enumerate(alist):
6+
if item == target:
7+
print("Found target {} at index {}".format(target, index))
8+
break
9+
else:
10+
print("Not found")
11+
12+
13+
def main():
14+
# Python 2's `raw_input` has been renamed to `input` in Python 3
15+
if sys.version_info.major < 3:
16+
input_function = raw_input
17+
else:
18+
input_function = input
19+
20+
try:
21+
print("Enter numbers separated by spaces")
22+
s = input_function()
23+
inputs = list(map(int, s.split(' ')))
24+
target = int(input_function('\nEnter a number to be found in list: '))
25+
except Exception as e:
26+
print(e)
27+
else:
28+
sequential_search(inputs, target)
29+
30+
if __name__ == '__main__':
31+
print('==== Linear Search ====\n')
32+
main()

MergeSort.py

+57-34
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
1-
def mergeSort(alist):
2-
print("Splitting ",alist)
3-
if len(alist)>1:
4-
mid = len(alist)//2
5-
lefthalf = alist[:mid]
6-
righthalf = alist[mid:]
7-
mergeSort(lefthalf)
8-
mergeSort(righthalf)
9-
i=0
10-
j=0
11-
k=0
12-
while i < len(lefthalf) and j < len(righthalf):
13-
if lefthalf[i] < righthalf[j]:
14-
alist[k]=lefthalf[i]
15-
i=i+1
1+
import sys
2+
3+
4+
def merge_sort(alist):
5+
print("Splitting ", alist)
6+
if len(alist) > 1:
7+
mid = len(alist) // 2
8+
left_half = alist[:mid]
9+
right_half = alist[mid:]
10+
merge_sort(left_half)
11+
merge_sort(right_half)
12+
i = j = k = 0
13+
14+
while i < len(left_half) and j < len(right_half):
15+
if left_half[i] < right_half[j]:
16+
alist[k] = left_half[i]
17+
i += 1
1618
else:
17-
alist[k]=righthalf[j]
18-
j=j+1
19-
k=k+1
20-
21-
while i < len(lefthalf):
22-
alist[k]=lefthalf[i]
23-
i=i+1
24-
k=k+1
25-
26-
while j < len(righthalf):
27-
alist[k]=righthalf[j]
28-
j=j+1
29-
k=k+1
30-
print("Merging ",alist)
31-
32-
print("Enter numbers seprated by space")
33-
s = input()
34-
numbers = list(map(int, s.split()))
35-
mergeSort(numbers)
19+
alist[k] = right_half[j]
20+
j += 1
21+
k += 1
22+
23+
while i < len(left_half):
24+
alist[k] = left_half[i]
25+
i += 1
26+
k += 1
27+
28+
while j < len(right_half):
29+
alist[k] = right_half[j]
30+
j += 1
31+
k += 1
32+
print("Merging ", alist)
33+
return alist
34+
35+
36+
def main():
37+
# Python 2's `raw_input` has been renamed to `input` in Python 3
38+
if sys.version_info.major < 3:
39+
input_function = raw_input
40+
else:
41+
input_function = input
42+
43+
try:
44+
print("Enter numbers separated by spaces:")
45+
s = input_function()
46+
inputs = list(map(int, s.split(' ')))
47+
if len(inputs) < 2:
48+
print('No Enough values to sort!')
49+
raise Exception
50+
51+
except Exception as e:
52+
print(e)
53+
else:
54+
sorted_input = merge_sort(inputs)
55+
print('\nSorted list (min to max): {}'.format(sorted_input))
3656

57+
if __name__ == '__main__':
58+
print('==== Merge Sort ====\n')
59+
main()

QuickSort.py

+29-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1+
import sys
12

2-
def quicksort(A, p, r):
3+
4+
def quick_sort(A, p, r):
35
if p < r:
46
q = partition(A, p, r)
5-
quicksort(A, p, q - 1)
6-
quicksort(A, q + 1, r)
7+
quick_sort(A, p, q - 1)
8+
quick_sort(A, q + 1, r)
9+
return A
710

811

912
def partition(A, p, r):
10-
x = A[r]
1113
i = p - 1
1214
for j in range(p, r):
13-
if A[j] <= x:
15+
if A[j] <= A[r]:
1416
i += 1
15-
tmp = A[i]
16-
A[i] = A[j]
17-
A[j] = tmp
18-
tmp = A[i+1]
19-
A[i+1] = A[r]
20-
A[r] = tmp
17+
A[i], A[j] = A[j], A[i]
18+
A[i + 1], A[r] = A[r], A[i + 1]
2119
return i + 1
2220

2321

24-
if __name__ == "__main__":
25-
print('Enter values seperated by space:')
26-
A = [int (item) for item in input().split(' ')]
27-
# A = [23, 45, 43, 12, 67, 98, 123, 99]
28-
# partition(A, 0, 7)
29-
print(A)
30-
quicksort(A, 0, 7)
31-
print(A)
22+
def main():
23+
# Python 2's `raw_input` has been renamed to `input` in Python 3
24+
if sys.version_info.major < 3:
25+
input_function = raw_input
26+
else:
27+
input_function = input
28+
29+
try:
30+
print("Enter numbers separated by spaces")
31+
s = input_function()
32+
inputs = list(map(int, s.split(' ')))
33+
except Exception as e:
34+
print(e)
35+
else:
36+
sorted_input = quick_sort(inputs, 0, len(inputs) - 1)
37+
print('\nSorted list (min to max): {}'.format(sorted_input))
38+
39+
if __name__ == '__main__':
40+
print('==== Quick Sort ====\n')
41+
main()

0 commit comments

Comments
 (0)