Skip to content

Commit 578845a

Browse files
author
Tony Sappe
committed
Improved MergeSort.py
* Added Python3 support
1 parent 37ddd2c commit 578845a

File tree

6 files changed

+121
-44
lines changed

6 files changed

+121
-44
lines changed

BubbleSort.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12

23

34
def simple_bubble_sort(int_list):
@@ -13,9 +14,15 @@ def simple_bubble_sort(int_list):
1314

1415

1516
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+
1623
try:
1724
print("Enter numbers separated by spaces:")
18-
s = raw_input()
25+
s = input_function()
1926
inputs = list(map(int, s.split(' ')))
2027
if len(inputs) < 2:
2128
print('No Enough values to sort!')

InsertionSort.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13

24
def simple_insertion_sort(int_list):
35
count = len(int_list)
@@ -13,9 +15,15 @@ def simple_insertion_sort(int_list):
1315

1416

1517
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
23+
1624
try:
1725
print("Enter numbers separated by spaces:")
18-
s = raw_input()
26+
s = input_function()
1927
inputs = list(map(int, s.split(' ')))
2028
if len(inputs) < 2:
2129
print('No Enough values to sort!')

LinearSearch.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13

24
def sequential_search(alist, target):
35
for index, item in enumerate(alist):
@@ -9,11 +11,17 @@ def sequential_search(alist, target):
911

1012

1113
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+
1220
try:
1321
print("Enter numbers separated by spaces")
14-
s = raw_input()
22+
s = input_function()
1523
inputs = list(map(int, s.split(' ')))
16-
target = int(raw_input('\nEnter a single number to be found in the list: '))
24+
target = int(input_function('\nEnter a number to be found in list: '))
1725
except Exception as e:
1826
print(e)
1927
else:

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13

24
def quick_sort(A, p, r):
35
if p < r:
@@ -18,9 +20,15 @@ def partition(A, p, r):
1820

1921

2022
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+
2129
try:
2230
print("Enter numbers separated by spaces")
23-
s = raw_input()
31+
s = input_function()
2432
inputs = list(map(int, s.split(' ')))
2533
except Exception as e:
2634
print(e)

README.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ __Properties__
1919
* Best case performance O(n)
2020
* Average case performance O(n^2)
2121

22-
2322
###### View the algorithm in [action][bubble-toptal]
2423

2524

26-
### Caesar
27-
Add comments here
2825

2926
### Insertion
3027
![alt text][insertion-image]
@@ -36,10 +33,22 @@ __Properties__
3633
* Best case performance O(n)
3734
* Average case performance O(n^2)
3835

39-
4036
###### View the algorithm in [action][insertion-toptal]
4137

4238

39+
## Merge
40+
![alt text][merge-image]
41+
42+
From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
43+
44+
__Properties__
45+
* Worst case performance O(n log n)
46+
* Best case performance O(n)
47+
* Average case performance O(n)
48+
49+
50+
###### View the algorithm in [action][merge-toptal]
51+
4352
## Quick
4453
![alt text][quick-image]
4554

@@ -50,9 +59,19 @@ __Properties__
5059
* Best case performance O(n log n) or O(n) with three-way partition
5160
* Average case performance O(n^2)
5261

53-
5462
###### View the algorithm in [action][quick-toptal]
5563

64+
65+
## Search Algorithms
66+
67+
### Linear
68+
Add comments here
69+
70+
## Ciphers
71+
72+
### Caesar
73+
Add comments here
74+
5675
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
5776
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
5877
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
@@ -64,3 +83,7 @@ __Properties__
6483
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
6584
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
6685
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"
86+
87+
[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort
88+
[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort
89+
[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort"

0 commit comments

Comments
 (0)