From cea6a35fb0012c404720f66d24c4e84259f52d11 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 12:36:11 +0000 Subject: [PATCH 1/7] verbose merge sort init --- pygorithm/sorting/merge_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index be1fc01..a410e49 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -48,6 +48,8 @@ def sort(_list): b = sort(_list[middle:]) return merge(a, b) +def sort_iter(_list): + pass # TODO: Are these necessary? def time_complexities(): From 5c0973e84dbe8096dcf4f5eee78bca8c2fa0da57 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 12:37:46 +0000 Subject: [PATCH 2/7] docstring added --- pygorithm/sorting/merge_sort.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index a410e49..7160c35 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -49,7 +49,14 @@ def sort(_list): return merge(a, b) def sort_iter(_list): - pass + """ + Function to sort an array + using merge sort algorithm, iteratively + + :param _list: list of values to sort + :return: sorted + """ + pass # TODO: Are these necessary? def time_complexities(): From 63b706e31e1c1abcbb792501904514c9e79b2aa4 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 13:45:09 +0000 Subject: [PATCH 3/7] added iterative code --- pygorithm/sorting/merge_sort.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index 7160c35..50ff6ef 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -48,7 +48,8 @@ def sort(_list): b = sort(_list[middle:]) return merge(a, b) -def sort_iter(_list): +from itertools import zip_longest +def sorti(_list): """ Function to sort an array using merge sort algorithm, iteratively @@ -56,7 +57,16 @@ def sort_iter(_list): :param _list: list of values to sort :return: sorted """ - pass + # breakdown every element into its own list + series = [[i] for i in _list] + while len(series) > 1: + # iterator to handle two at a time in the zip_longest below + isl = iter(series) + series = [ + merge(a, b) if b else a + for a, b in zip_longest(isl, isl) + ] + return series[0] # TODO: Are these necessary? def time_complexities(): From d70c319ffee40998049712aaa9d26f3580ac16b5 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 16:33:13 +0000 Subject: [PATCH 4/7] added test for iterative mergesort --- pygorithm/sorting/merge_sort.py | 1 + tests/test_sorting.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index 50ff6ef..8e50e27 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -60,6 +60,7 @@ def sorti(_list): # breakdown every element into its own list series = [[i] for i in _list] while len(series) > 1: + print(series) # iterator to handle two at a time in the zip_longest below isl = iter(series) series = [ diff --git a/tests/test_sorting.py b/tests/test_sorting.py index f7fcf6d..e7acb99 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -116,6 +116,13 @@ class TestMergeSort(unittest.TestCase, TestSortingAlgorithm): def sort(arr): return merge_sort.sort(arr) +class TestMergeSortIterative(unittest.TestCase, TestSortingAlgorithm): + inplace = False + alph_support = True + + @staticmethod + def sort(arr): + return merge_sort.sorti(arr) class TestQuickSort(unittest.TestCase, TestSortingAlgorithm): inplace = False From 395c2cd29c3817fd6f16b35aa64575b8301cd3c3 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 16:39:13 +0000 Subject: [PATCH 5/7] added verbose option --- pygorithm/sorting/merge_sort.py | 4 ++-- tests/test_sorting.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index 8e50e27..b41fcf4 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -49,7 +49,7 @@ def sort(_list): return merge(a, b) from itertools import zip_longest -def sorti(_list): +def sorti(_list, verbose=True): """ Function to sort an array using merge sort algorithm, iteratively @@ -60,7 +60,7 @@ def sorti(_list): # breakdown every element into its own list series = [[i] for i in _list] while len(series) > 1: - print(series) + if verbose: print(series) # iterator to handle two at a time in the zip_longest below isl = iter(series) series = [ diff --git a/tests/test_sorting.py b/tests/test_sorting.py index e7acb99..c9c650c 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -122,7 +122,7 @@ class TestMergeSortIterative(unittest.TestCase, TestSortingAlgorithm): @staticmethod def sort(arr): - return merge_sort.sorti(arr) + return merge_sort.sorti(arr, verbose=False) class TestQuickSort(unittest.TestCase, TestSortingAlgorithm): inplace = False From 88d7ca32e5fdb3fae3e7e591cb0d16ede0eadf2b Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 16:54:23 +0000 Subject: [PATCH 6/7] get_code has option to get iterative code --- pygorithm/sorting/merge_sort.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index b41fcf4..df35117 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -79,11 +79,14 @@ def time_complexities(): return "Best Case: O(nlogn), Average Case: O(nlogn), Worst Case: O(nlogn)" -def get_code(): +def get_code(iter=False): """ easily retrieve the source code of the sort function :return: source code """ + if iter: + return inspect.getsource(sorti) + "\n" + inspect.getsource(merge) + return inspect.getsource(sort) + "\n" + inspect.getsource(merge) From e0b5ae368b51d4514082064041f1dd94249e7f3f Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Thu, 27 Aug 2020 16:57:12 +0000 Subject: [PATCH 7/7] get_code has option to get iterative code --- pygorithm/sorting/merge_sort.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygorithm/sorting/merge_sort.py b/pygorithm/sorting/merge_sort.py index b41fcf4..f4c21a3 100644 --- a/pygorithm/sorting/merge_sort.py +++ b/pygorithm/sorting/merge_sort.py @@ -79,11 +79,14 @@ def time_complexities(): return "Best Case: O(nlogn), Average Case: O(nlogn), Worst Case: O(nlogn)" -def get_code(): +def get_code(iter=False): """ easily retrieve the source code of the sort function :return: source code """ + if iter: + return inspect.getsource(sorti) + "\n" + return inspect.getsource(sort) + "\n" + inspect.getsource(merge)