Improved MergeSort.py

* Added Python3 support
This commit is contained in:
Tony Sappe 2016-07-29 15:48:47 -04:00
parent 37ddd2c8d0
commit 578845a6b5
6 changed files with 118 additions and 41 deletions

View File

@ -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!')

View File

@ -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!')

View File

@ -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:

View File

@ -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()

View File

@ -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)

View File

@ -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"