From 702a0ccb63efd6b6bf17271dbd10ae3c5a9064fb Mon Sep 17 00:00:00 2001 From: amangupta0709 Date: Mon, 11 May 2020 07:23:24 +0530 Subject: [PATCH 1/4] Added Iterative merge sort --- sorts/iterative_merge_sort.py | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 sorts/iterative_merge_sort.py diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py new file mode 100644 index 000000000000..f627170f8d91 --- /dev/null +++ b/sorts/iterative_merge_sort.py @@ -0,0 +1,46 @@ +""" +Implementation of iterative merge sort in python +Author: Aman Gupta + +""" + +def merge(low,mid,high,inputlist): + """ + sorting left-half and right-half individually + then merging them into result + """ + result = [] + left , right = inputlist[low:mid] , inputlist[mid:high+1] + while left and right: + result.append((left if left[0] <= right[0] else right).pop(0)) + inputlist[low:high+1] = result + left + right + return inputlist + +# iteration over the unsorted list +def iter_merge_sort(inputlist): + + + if len(inputlist) <= 1: + return inputlist + + # iteration for two-way merging + p=2 + while(p=len(inputlist)): + mid = i + inputlist = merge(0,mid,len(inputlist)-1,inputlist) + + p *= 2 + + return inputlist + +if __name__ == "__main__": + unsorted = list(map(int,input("Enter numbers separated by a comma:\n").split(','))) + print(*iter_merge_sort(unsorted), sep=",") From ebb1ad1b4ce9cdd312ef80f16e5c6303e5ab8d37 Mon Sep 17 00:00:00 2001 From: amangupta0709 Date: Mon, 11 May 2020 08:14:01 +0530 Subject: [PATCH 2/4] Added iterative merge sorts --- sorts/iterative_merge_sort.py | 59 +++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py index f627170f8d91..b19dde33defa 100644 --- a/sorts/iterative_merge_sort.py +++ b/sorts/iterative_merge_sort.py @@ -1,46 +1,73 @@ """ -Implementation of iterative merge sort in python +This is pure implementation of iterative merge sort in python Author: Aman Gupta +For doctests run following command: +python -m doctest -v shell_sort.py +or +python3 -m doctest -v shell_sort.py + +For manual testing run: +python shell_sort.py """ -def merge(low,mid,high,inputlist): +from typing import List + + +def merge(low: int, mid: int, high: int, inputlist: List) -> List: """ sorting left-half and right-half individually then merging them into result """ result = [] - left , right = inputlist[low:mid] , inputlist[mid:high+1] + left, right = inputlist[low:mid], inputlist[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0)) - inputlist[low:high+1] = result + left + right + inputlist[low : high + 1] = result + left + right return inputlist + # iteration over the unsorted list -def iter_merge_sort(inputlist): +def iter_merge_sort(inputlist: List) -> List: + """ + Returns sorted list of the input numbers list + Examples + + >>> iter_merge_sort([5,9,8,1,2,7]) + [1, 2, 5, 7, 8, 9] + + >>> iter_merge_sort([6]) + [6] + + >>> iter_merge_sort([-2,-9,-1,-4]) + [-9, -4, -2, -1] + + """ if len(inputlist) <= 1: return inputlist # iteration for two-way merging - p=2 - while(p=len(inputlist)): + high = i + p - 1 + mid = (low + high + 1) // 2 + inputlist = merge(low, mid, high, inputlist) + # final merge of last two parts + if p * 2 >= len(inputlist): mid = i - inputlist = merge(0,mid,len(inputlist)-1,inputlist) + inputlist = merge(0, mid, len(inputlist) - 1, inputlist) p *= 2 return inputlist + if __name__ == "__main__": - unsorted = list(map(int,input("Enter numbers separated by a comma:\n").split(','))) + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(*iter_merge_sort(unsorted), sep=",") From 1d41c188503c03ea41da59ccc62302e8d71bcae8 Mon Sep 17 00:00:00 2001 From: amangupta0709 Date: Mon, 11 May 2020 17:55:32 +0530 Subject: [PATCH 3/4] Update changes --- sorts/iterative_merge_sort.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py index b19dde33defa..cfd3a0ea8724 100644 --- a/sorts/iterative_merge_sort.py +++ b/sorts/iterative_merge_sort.py @@ -3,18 +3,16 @@ Author: Aman Gupta For doctests run following command: -python -m doctest -v shell_sort.py -or -python3 -m doctest -v shell_sort.py +python3 -m doctest -v iterative_merge_sort.py For manual testing run: -python shell_sort.py +python3 iterative_merge_sort.py """ from typing import List -def merge(low: int, mid: int, high: int, inputlist: List) -> List: +def merge(inputlist: List, low: int, mid: int, high: int) -> List: """ sorting left-half and right-half individually then merging them into result @@ -40,6 +38,9 @@ def iter_merge_sort(inputlist: List) -> List: >>> iter_merge_sort([6]) [6] + >>> iter_merge_sort([]) + [] + >>> iter_merge_sort([-2,-9,-1,-4]) [-9, -4, -2, -1] @@ -56,11 +57,11 @@ def iter_merge_sort(inputlist: List) -> List: low = i high = i + p - 1 mid = (low + high + 1) // 2 - inputlist = merge(low, mid, high, inputlist) + inputlist = merge(inputlist, low, mid, high) # final merge of last two parts if p * 2 >= len(inputlist): mid = i - inputlist = merge(0, mid, len(inputlist) - 1, inputlist) + inputlist = merge(inputlist, 0, mid, len(inputlist) - 1) p *= 2 @@ -69,5 +70,5 @@ def iter_merge_sort(inputlist: List) -> List: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item) for item in user_input.split(",")] + unsorted = [int(item.strip()) for item in user_input.split(",")] print(*iter_merge_sort(unsorted), sep=",") From 4e41a6000630870c2c1ec379b61873f22966697d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 11 May 2020 16:27:10 +0200 Subject: [PATCH 4/4] Add the ability to sort strings --- sorts/iterative_merge_sort.py | 53 +++++++++++++++++------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py index cfd3a0ea8724..e6e1513393e0 100644 --- a/sorts/iterative_merge_sort.py +++ b/sorts/iterative_merge_sort.py @@ -1,5 +1,5 @@ """ -This is pure implementation of iterative merge sort in python +Implementation of iterative merge sort in Python Author: Aman Gupta For doctests run following command: @@ -12,63 +12,62 @@ from typing import List -def merge(inputlist: List, low: int, mid: int, high: int) -> List: +def merge(input_list: List, low: int, mid: int, high: int) -> List: """ sorting left-half and right-half individually then merging them into result """ result = [] - left, right = inputlist[low:mid], inputlist[mid : high + 1] + left, right = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0)) - inputlist[low : high + 1] = result + left + right - return inputlist + input_list[low : high + 1] = result + left + right + return input_list # iteration over the unsorted list -def iter_merge_sort(inputlist: List) -> List: +def iter_merge_sort(input_list: List) -> List: """ - Returns sorted list of the input numbers list - - Examples - - >>> iter_merge_sort([5,9,8,1,2,7]) - [1, 2, 5, 7, 8, 9] + Return a sorted copy of the input list + >>> iter_merge_sort([5, 9, 8, 7, 1, 2, 7]) + [1, 2, 5, 7, 7, 8, 9] >>> iter_merge_sort([6]) [6] - >>> iter_merge_sort([]) [] - - >>> iter_merge_sort([-2,-9,-1,-4]) + >>> iter_merge_sort([-2, -9, -1, -4]) [-9, -4, -2, -1] - + >>> iter_merge_sort([1.1, 1, 0.0, -1, -1.1]) + [-1.1, -1, 0.0, 1, 1.1] + >>> iter_merge_sort(['c', 'b', 'a']) + ['a', 'b', 'c'] + >>> iter_merge_sort('cba') + ['a', 'b', 'c'] """ - - if len(inputlist) <= 1: - return inputlist + if len(input_list) <= 1: + return input_list + input_list = list(input_list) # iteration for two-way merging p = 2 - while p < len(inputlist): + while p < len(input_list): # getting low, high and middle value for merge-sort of single list - for i in range(0, len(inputlist), p): + for i in range(0, len(input_list), p): low = i high = i + p - 1 mid = (low + high + 1) // 2 - inputlist = merge(inputlist, low, mid, high) + input_list = merge(input_list, low, mid, high) # final merge of last two parts - if p * 2 >= len(inputlist): + if p * 2 >= len(input_list): mid = i - inputlist = merge(inputlist, 0, mid, len(inputlist) - 1) - + input_list = merge(input_list, 0, mid, len(input_list) - 1) p *= 2 - return inputlist + return input_list if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item.strip()) for item in user_input.split(",")] - print(*iter_merge_sort(unsorted), sep=",") + print(iter_merge_sort(unsorted))