From 5bd08314bdaa6f2f84f168c210e28a66f85c93b8 Mon Sep 17 00:00:00 2001 From: AHartNtkn Date: Mon, 13 Apr 2020 12:07:41 -0700 Subject: [PATCH 1/5] Itterative sorting implementations. --- src/iterative_sorting/iterative_sorting.py | 44 ++++++++++++---------- src/iterative_sorting/test_iterative.py | 20 +++++----- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..71674a21 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,36 @@ -# TO-DO: Complete the selection_sort() function below def selection_sort( arr ): - # loop through n-1 elements - for i in range(0, len(arr) - 1): - cur_index = i - smallest_index = cur_index - # TO-DO: find next smallest element - # (hint, can do in 3 loc) - - - - - # TO-DO: swap - + for i in range(len(arr)): + sm_i = min(enumerate(arr[i:], start=i), key=lambda x: x[1])[0] + arr[i], arr[sm_i] = arr[sm_i], arr[i] + return arr +def bubble_sort( arr ): + sorted = False + while not sorted: + sorted = True + for i in range(len(arr)-1): + if arr[i] > arr[i+1]: + sorted = False + arr[i], arr[i+1] = arr[i+1], arr[i] return arr +def count_sort( arr, maximum=-1 ): + if arr == []: + return [] -# TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): + if min(arr) < 0: + return 'Error, negative numbers not allowed in Count Sort' - return arr + cntArray = [0] * (max(arr)+1) + for e in arr: + cntArray[e]+=1 -# STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): + index = 0 + for i, e in enumerate(cntArray): + arr[index:index+e] = [i]*e + index+=e - return arr \ No newline at end of file + return arr diff --git a/src/iterative_sorting/test_iterative.py b/src/iterative_sorting/test_iterative.py index df11b648..468d92d7 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -26,16 +26,16 @@ def test_bubble_sort(self): self.assertEqual(bubble_sort(arr4), sorted(arr4)) # Uncomment this test to test your count_sort implementation - # def test_counting_sort(self): - # arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] - # arr2 = [] - # arr3 = [1, 5, -2, 4, 3] - # arr4 = random.sample(range(200), 50) - - # self.assertEqual(count_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) - # self.assertEqual(count_sort(arr2), []) - # self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort") - # self.assertEqual(count_sort(arr4), sorted(arr4)) + def test_counting_sort(self): + arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] + arr2 = [] + arr3 = [1, 5, -2, 4, 3] + arr4 = random.sample(range(200), 50) + + self.assertEqual(count_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) + self.assertEqual(count_sort(arr2), []) + self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort") + self.assertEqual(count_sort(arr4), sorted(arr4)) if __name__ == '__main__': From 3820b5102293934fdbcb233682acfd62677f0bf2 Mon Sep 17 00:00:00 2001 From: AHartNtkn Date: Mon, 13 Apr 2020 12:08:38 -0700 Subject: [PATCH 2/5] More consistent names. --- src/iterative_sorting/iterative_sorting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 71674a21..d0acdf02 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -23,13 +23,13 @@ def count_sort( arr, maximum=-1 ): if min(arr) < 0: return 'Error, negative numbers not allowed in Count Sort' - cntArray = [0] * (max(arr)+1) + cntArr = [0] * (max(arr)+1) for e in arr: - cntArray[e]+=1 + cntArr[e]+=1 index = 0 - for i, e in enumerate(cntArray): + for i, e in enumerate(cntArr): arr[index:index+e] = [i]*e index+=e From f792c9262561ba90b5b2000a1245ce81c24fecb1 Mon Sep 17 00:00:00 2001 From: AHartNtkn Date: Mon, 13 Apr 2020 12:12:10 -0700 Subject: [PATCH 3/5] Add comment and correct indentation. --- src/iterative_sorting/iterative_sorting.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index d0acdf02..611c01a7 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -8,11 +8,12 @@ def selection_sort( arr ): def bubble_sort( arr ): sorted = False while not sorted: - sorted = True - for i in range(len(arr)-1): - if arr[i] > arr[i+1]: - sorted = False - arr[i], arr[i+1] = arr[i+1], arr[i] + # Assume it's already sorted and look for evidence to the contrary. + sorted = True + for i in range(len(arr)-1): + if arr[i] > arr[i+1]: + sorted = False + arr[i], arr[i+1] = arr[i+1], arr[i] return arr From 39eb42f3bc82e40a7a0123617e255979e67a0bd6 Mon Sep 17 00:00:00 2001 From: AHartNtkn Date: Mon, 13 Apr 2020 18:26:42 -0700 Subject: [PATCH 4/5] Implement merge sort and minor improvement. --- src/iterative_sorting/iterative_sorting.py | 2 +- src/recursive_sorting/recursive_sorting.py | 39 ++++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 611c01a7..008acc9c 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,5 +1,5 @@ def selection_sort( arr ): - for i in range(len(arr)): + for i in range(len(arr)-1): sm_i = min(enumerate(arr[i:], start=i), key=lambda x: x[1])[0] arr[i], arr[sm_i] = arr[sm_i], arr[i] diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..27e138f7 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -2,16 +2,49 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements - # TO-DO + iM, iA, iB = 0, 0, 0 + while iA < len( arrA ) and iB < len( arrB ): + if arrA[iA] < arrB[iB]: + merged_arr[iM] = arrA[iA] + iA += 1 + iM += 1 + else: + merged_arr[iM] = arrB[iB] + iB += 1 + iM += 1 + + if iA == len( arrA ): + merged_arr[iM:] = arrB[iB:] + else: + merged_arr[iM:] = arrA[iA:] + return merged_arr # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): - # TO-DO + if arr == []: + return [] - return arr + if not isinstance(arr[0], list): + arr = list(map(lambda x: [x], arr)) + + if len(arr) == 1: + return arr[0] + + if len(arr) % 2 == 0: + split1 = arr[:len(arr)//2] + split2 = arr[len(arr)//2:] + rem = [] + else: + split1 = arr[:len(arr)//2] + split2 = arr[len(arr)//2:-1] + rem = [arr[-1]] + + merged = [ merge(a, b) for a, b in zip(split1, split2) ] + rem + + return merge_sort(merged) # STRETCH: implement an in-place merge sort algorithm From 1657335b9003302e754b2d02981097fd00fd375d Mon Sep 17 00:00:00 2001 From: AHartNtkn Date: Tue, 14 Apr 2020 09:04:55 -0700 Subject: [PATCH 5/5] More efficient bubblesort. --- src/iterative_sorting/iterative_sorting.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 008acc9c..8b4caeb4 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -6,14 +6,13 @@ def selection_sort( arr ): return arr def bubble_sort( arr ): - sorted = False - while not sorted: - # Assume it's already sorted and look for evidence to the contrary. - sorted = True - for i in range(len(arr)-1): + elem_num = len(arr) + sorted_elems = 0 + while sorted_elems != elem_num: + for i in range(elem_num - sorted_elems - 1): if arr[i] > arr[i+1]: - sorted = False arr[i], arr[i+1] = arr[i+1], arr[i] + sorted_elems += 1 return arr