forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Python3 support
- Loading branch information
Tony Sappe
committed
Jul 29, 2016
1 parent
37ddd2c
commit 578845a
Showing
6 changed files
with
121 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters