From 578845a6b5491c797ddad8eb61297e9f6383962c Mon Sep 17 00:00:00 2001 From: Tony Sappe Date: Fri, 29 Jul 2016 15:48:47 -0400 Subject: [PATCH] Improved MergeSort.py * Added Python3 support --- BubbleSort.py | 9 ++++- InsertionSort.py | 10 +++++- LinearSearch.py | 12 +++++-- MergeSort.py | 85 ++++++++++++++++++++++++++++++------------------ QuickSort.py | 10 +++++- README.md | 33 ++++++++++++++++--- 6 files changed, 118 insertions(+), 41 deletions(-) diff --git a/BubbleSort.py b/BubbleSort.py index d74b5b223..9f3579617 100644 --- a/BubbleSort.py +++ b/BubbleSort.py @@ -1,3 +1,4 @@ +import sys def simple_bubble_sort(int_list): @@ -13,9 +14,15 @@ def simple_bubble_sort(int_list): def main(): + # Python 2's `raw_input` has been renamed to `input` in Python 3 + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + try: print("Enter numbers separated by spaces:") - s = raw_input() + s = input_function() inputs = list(map(int, s.split(' '))) if len(inputs) < 2: print('No Enough values to sort!') diff --git a/InsertionSort.py b/InsertionSort.py index 74cbc57e8..a577c4a81 100644 --- a/InsertionSort.py +++ b/InsertionSort.py @@ -1,3 +1,5 @@ +import sys + def simple_insertion_sort(int_list): count = len(int_list) @@ -13,9 +15,15 @@ def simple_insertion_sort(int_list): def main(): + # Python 2's `raw_input` has been renamed to `input` in Python 3 + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + try: print("Enter numbers separated by spaces:") - s = raw_input() + s = input_function() inputs = list(map(int, s.split(' '))) if len(inputs) < 2: print('No Enough values to sort!') diff --git a/LinearSearch.py b/LinearSearch.py index 61c9e0dd5..82595eded 100644 --- a/LinearSearch.py +++ b/LinearSearch.py @@ -1,3 +1,5 @@ +import sys + def sequential_search(alist, target): for index, item in enumerate(alist): @@ -9,11 +11,17 @@ def sequential_search(alist, target): def main(): + # Python 2's `raw_input` has been renamed to `input` in Python 3 + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + try: print("Enter numbers separated by spaces") - s = raw_input() + s = input_function() inputs = list(map(int, s.split(' '))) - target = int(raw_input('\nEnter a single number to be found in the list: ')) + target = int(input_function('\nEnter a number to be found in list: ')) except Exception as e: print(e) else: diff --git a/MergeSort.py b/MergeSort.py index 7ce144dc7..ff3a31c70 100644 --- a/MergeSort.py +++ b/MergeSort.py @@ -1,36 +1,59 @@ -def mergeSort(alist): - print("Splitting ",alist) - if len(alist)>1: - mid = len(alist)//2 - lefthalf = alist[:mid] - righthalf = alist[mid:] - mergeSort(lefthalf) - mergeSort(righthalf) - i=0 - j=0 - k=0 - while i < len(lefthalf) and j < len(righthalf): - if lefthalf[i] < righthalf[j]: - alist[k]=lefthalf[i] - i=i+1 +import sys + + +def merge_sort(alist): + print("Splitting ", alist) + if len(alist) > 1: + mid = len(alist) // 2 + left_half = alist[:mid] + right_half = alist[mid:] + merge_sort(left_half) + merge_sort(right_half) + i = j = k = 0 + + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + alist[k] = left_half[i] + i += 1 else: - alist[k]=righthalf[j] - j=j+1 - k=k+1 + alist[k] = right_half[j] + j += 1 + k += 1 - while i < len(lefthalf): - alist[k]=lefthalf[i] - i=i+1 - k=k+1 + while i < len(left_half): + alist[k] = left_half[i] + i += 1 + k += 1 - while j < len(righthalf): - alist[k]=righthalf[j] - j=j+1 - k=k+1 - print("Merging ",alist) + while j < len(right_half): + alist[k] = right_half[j] + j += 1 + k += 1 + print("Merging ", alist) + return alist -print("Enter numbers seprated by space") -s = input() -numbers = list(map(int, s.split())) -mergeSort(numbers) +def main(): + # Python 2's `raw_input` has been renamed to `input` in Python 3 + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + + try: + print("Enter numbers separated by spaces:") + s = input_function() + inputs = list(map(int, s.split(' '))) + if len(inputs) < 2: + print('No Enough values to sort!') + raise Exception + + except Exception as e: + print(e) + else: + sorted_input = merge_sort(inputs) + print('\nSorted list (min to max): {}'.format(sorted_input)) + +if __name__ == '__main__': + print('==== Merge Sort ====\n') + main() diff --git a/QuickSort.py b/QuickSort.py index 7221cfe75..2d922a3de 100644 --- a/QuickSort.py +++ b/QuickSort.py @@ -1,3 +1,5 @@ +import sys + def quick_sort(A, p, r): if p < r: @@ -18,9 +20,15 @@ def partition(A, p, r): def main(): + # Python 2's `raw_input` has been renamed to `input` in Python 3 + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + try: print("Enter numbers separated by spaces") - s = raw_input() + s = input_function() inputs = list(map(int, s.split(' '))) except Exception as e: print(e) diff --git a/README.md b/README.md index b44e12e93..65f0c70da 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,9 @@ __Properties__ * Best case performance O(n) * Average case performance O(n^2) - ###### View the algorithm in [action][bubble-toptal] -### Caesar -Add comments here ### Insertion ![alt text][insertion-image] @@ -36,10 +33,22 @@ __Properties__ * Best case performance O(n) * Average case performance O(n^2) - ###### View the algorithm in [action][insertion-toptal] +## Merge +![alt text][merge-image] + +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. + +__Properties__ +* Worst case performance O(n log n) +* Best case performance O(n) +* Average case performance O(n) + + +###### View the algorithm in [action][merge-toptal] + ## Quick ![alt text][quick-image] @@ -50,9 +59,19 @@ __Properties__ * Best case performance O(n log n) or O(n) with three-way partition * Average case performance O(n^2) - ###### View the algorithm in [action][quick-toptal] + +## Search Algorithms + +### Linear +Add comments here + +## Ciphers + +### Caesar +Add comments here + [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort [bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort [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__ [quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort [quick-wiki]: https://en.wikipedia.org/wiki/Quicksort [quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort" + +[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort +[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort +[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort"