mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 14:13:44 +00:00
Improved MergeSort.py
* Added Python3 support
This commit is contained in:
parent
37ddd2c8d0
commit
578845a6b5
|
@ -1,3 +1,4 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def simple_bubble_sort(int_list):
|
def simple_bubble_sort(int_list):
|
||||||
|
@ -13,9 +14,15 @@ def simple_bubble_sort(int_list):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
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:
|
try:
|
||||||
print("Enter numbers separated by spaces:")
|
print("Enter numbers separated by spaces:")
|
||||||
s = raw_input()
|
s = input_function()
|
||||||
inputs = list(map(int, s.split(' ')))
|
inputs = list(map(int, s.split(' ')))
|
||||||
if len(inputs) < 2:
|
if len(inputs) < 2:
|
||||||
print('No Enough values to sort!')
|
print('No Enough values to sort!')
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def simple_insertion_sort(int_list):
|
def simple_insertion_sort(int_list):
|
||||||
count = len(int_list)
|
count = len(int_list)
|
||||||
|
@ -13,9 +15,15 @@ def simple_insertion_sort(int_list):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
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:
|
try:
|
||||||
print("Enter numbers separated by spaces:")
|
print("Enter numbers separated by spaces:")
|
||||||
s = raw_input()
|
s = input_function()
|
||||||
inputs = list(map(int, s.split(' ')))
|
inputs = list(map(int, s.split(' ')))
|
||||||
if len(inputs) < 2:
|
if len(inputs) < 2:
|
||||||
print('No Enough values to sort!')
|
print('No Enough values to sort!')
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def sequential_search(alist, target):
|
def sequential_search(alist, target):
|
||||||
for index, item in enumerate(alist):
|
for index, item in enumerate(alist):
|
||||||
|
@ -9,11 +11,17 @@ def sequential_search(alist, target):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
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:
|
try:
|
||||||
print("Enter numbers separated by spaces")
|
print("Enter numbers separated by spaces")
|
||||||
s = raw_input()
|
s = input_function()
|
||||||
inputs = list(map(int, s.split(' ')))
|
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:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
else:
|
else:
|
||||||
|
|
85
MergeSort.py
85
MergeSort.py
|
@ -1,36 +1,59 @@
|
||||||
def mergeSort(alist):
|
import sys
|
||||||
print("Splitting ",alist)
|
|
||||||
if len(alist)>1:
|
|
||||||
mid = len(alist)//2
|
def merge_sort(alist):
|
||||||
lefthalf = alist[:mid]
|
print("Splitting ", alist)
|
||||||
righthalf = alist[mid:]
|
if len(alist) > 1:
|
||||||
mergeSort(lefthalf)
|
mid = len(alist) // 2
|
||||||
mergeSort(righthalf)
|
left_half = alist[:mid]
|
||||||
i=0
|
right_half = alist[mid:]
|
||||||
j=0
|
merge_sort(left_half)
|
||||||
k=0
|
merge_sort(right_half)
|
||||||
while i < len(lefthalf) and j < len(righthalf):
|
i = j = k = 0
|
||||||
if lefthalf[i] < righthalf[j]:
|
|
||||||
alist[k]=lefthalf[i]
|
while i < len(left_half) and j < len(right_half):
|
||||||
i=i+1
|
if left_half[i] < right_half[j]:
|
||||||
|
alist[k] = left_half[i]
|
||||||
|
i += 1
|
||||||
else:
|
else:
|
||||||
alist[k]=righthalf[j]
|
alist[k] = right_half[j]
|
||||||
j=j+1
|
j += 1
|
||||||
k=k+1
|
k += 1
|
||||||
|
|
||||||
while i < len(lefthalf):
|
while i < len(left_half):
|
||||||
alist[k]=lefthalf[i]
|
alist[k] = left_half[i]
|
||||||
i=i+1
|
i += 1
|
||||||
k=k+1
|
k += 1
|
||||||
|
|
||||||
while j < len(righthalf):
|
while j < len(right_half):
|
||||||
alist[k]=righthalf[j]
|
alist[k] = right_half[j]
|
||||||
j=j+1
|
j += 1
|
||||||
k=k+1
|
k += 1
|
||||||
print("Merging ",alist)
|
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()
|
||||||
|
|
10
QuickSort.py
10
QuickSort.py
|
@ -1,3 +1,5 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def quick_sort(A, p, r):
|
def quick_sort(A, p, r):
|
||||||
if p < r:
|
if p < r:
|
||||||
|
@ -18,9 +20,15 @@ def partition(A, p, r):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
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:
|
try:
|
||||||
print("Enter numbers separated by spaces")
|
print("Enter numbers separated by spaces")
|
||||||
s = raw_input()
|
s = input_function()
|
||||||
inputs = list(map(int, s.split(' ')))
|
inputs = list(map(int, s.split(' ')))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
33
README.md
33
README.md
|
@ -19,12 +19,9 @@ __Properties__
|
||||||
* Best case performance O(n)
|
* Best case performance O(n)
|
||||||
* Average case performance O(n^2)
|
* Average case performance O(n^2)
|
||||||
|
|
||||||
|
|
||||||
###### View the algorithm in [action][bubble-toptal]
|
###### View the algorithm in [action][bubble-toptal]
|
||||||
|
|
||||||
|
|
||||||
### Caesar
|
|
||||||
Add comments here
|
|
||||||
|
|
||||||
### Insertion
|
### Insertion
|
||||||
![alt text][insertion-image]
|
![alt text][insertion-image]
|
||||||
|
@ -36,10 +33,22 @@ __Properties__
|
||||||
* Best case performance O(n)
|
* Best case performance O(n)
|
||||||
* Average case performance O(n^2)
|
* Average case performance O(n^2)
|
||||||
|
|
||||||
|
|
||||||
###### View the algorithm in [action][insertion-toptal]
|
###### 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
|
## Quick
|
||||||
![alt text][quick-image]
|
![alt text][quick-image]
|
||||||
|
|
||||||
|
@ -50,9 +59,19 @@ __Properties__
|
||||||
* Best case performance O(n log n) or O(n) with three-way partition
|
* Best case performance O(n log n) or O(n) with three-way partition
|
||||||
* Average case performance O(n^2)
|
* Average case performance O(n^2)
|
||||||
|
|
||||||
|
|
||||||
###### View the algorithm in [action][quick-toptal]
|
###### 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-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
|
||||||
[bubble-wiki]: https://en.wikipedia.org/wiki/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"
|
[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-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
|
||||||
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
|
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
|
||||||
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"
|
[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"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user