Skip to content

Commit

Permalink
Improved MergeSort.py
Browse files Browse the repository at this point in the history
* Added Python3 support
  • Loading branch information
Tony Sappe committed Jul 29, 2016
1 parent 37ddd2c commit 578845a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 44 deletions.
9 changes: 8 additions & 1 deletion BubbleSort.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys


def simple_bubble_sort(int_list):
Expand All @@ -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!')
Expand Down
10 changes: 9 additions & 1 deletion InsertionSort.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys


def simple_insertion_sort(int_list):
count = len(int_list)
Expand All @@ -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!')
Expand Down
12 changes: 10 additions & 2 deletions LinearSearch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys


def sequential_search(alist, target):
for index, item in enumerate(alist):
Expand All @@ -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:
Expand Down
91 changes: 57 additions & 34 deletions MergeSort.py
Original file line number Diff line number Diff line change
@@ -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

while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1

while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)

print("Enter numbers seprated by space")
s = input()
numbers = list(map(int, s.split()))
mergeSort(numbers)
alist[k] = right_half[j]
j += 1
k += 1

while i < len(left_half):
alist[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):
alist[k] = right_half[j]
j += 1
k += 1
print("Merging ", alist)
return alist


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()
10 changes: 9 additions & 1 deletion QuickSort.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys


def quick_sort(A, p, r):
if p < r:
Expand All @@ -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)
Expand Down
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]

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

0 comments on commit 578845a

Please sign in to comment.