From 05c9f8b9fc7115c8e5d4d6f3aaa0f9b6f479f40f Mon Sep 17 00:00:00 2001 From: DNason1999 <50815684+DNason1999@users.noreply.github.com> Date: Mon, 13 Apr 2020 11:39:56 -0500 Subject: [PATCH 1/2] Part 1 Finished Part 1 and the stetch goal in iterative_sorting --- src/iterative_sorting/iterative_sorting.py | 41 ++++++++++++++++++---- src/iterative_sorting/test_iterative.py | 20 +++++------ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..e76cf8d7 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -6,25 +6,52 @@ def selection_sort( arr ): smallest_index = cur_index # TO-DO: find next smallest element # (hint, can do in 3 loc) - - - - + for x in range(i,len(arr)): + if(arr[x] < arr[smallest_index]): + smallest_index = x + # TO-DO: swap - - - + t = arr[cur_index] + arr[cur_index] = arr[smallest_index] + arr[smallest_index] = t return arr # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): + for i in range(0, len( arr )-1): + for x in range(0, len( arr )-1): + if(arr[x] > arr[x+1]): + t = arr[x] + arr[x] = arr[x+1] + arr[x+1] = t return arr # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): + if(len(arr) == 0): + return arr + elif(any([True if x < 0 else False for x in arr])): + return "Error, negative numbers not allowed in Count Sort" + + # Make counting list of length = largest integer in arr + count_arr = [0]*(max(arr)+1) + out_arr = [0]*(len(arr)) + + # Count instances of each numberin the counting list + for x in arr: + count_arr[x] = count_arr[x] + 1 + + for x in range(0,len(count_arr)-1): + count_arr[x+1] = count_arr[x+1] + count_arr[x] + + for x in arr[::-1]: + index = count_arr[x] - 1 + out_arr[index] = x + + arr = out_arr return arr \ No newline at end of file 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 c3b705d10096651268e1c9d932169670d07c96f1 Mon Sep 17 00:00:00 2001 From: DNason1999 <50815684+DNason1999@users.noreply.github.com> Date: Mon, 13 Apr 2020 13:18:48 -0500 Subject: [PATCH 2/2] Part 2 Completion of Part 2 MVP and stretch goal --- src/recursive_sorting/recursive_sorting.py | 49 +++++++++++++++++++++- src/recursive_sorting/test_recursive.py | 22 +++++----- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..9c4f4489 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,15 +1,48 @@ -# TO-DO: complete the helpe function below to merge 2 sorted arrays +# TO-DO: complete the helper function below to merge 2 sorted arrays def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO + cA, cB, cM = 0,0,0 + while(cA < len(arrA) and cB < len(arrB)): + if(arrA[cA] < arrB[cB]): + merged_arr[cM] = arrA[cA] + cA += 1 + else: + merged_arr[cM] = arrB[cB] + cB += 1 + cM += 1 + + if(cA < len(arrA)): + while(cM < len(merged_arr)): + merged_arr[cM] = arrA[cA] + cM +=1 + cA +=1 + + if(cB < len(arrB)): + while(cM < len(merged_arr)): + merged_arr[cM] = arrB[cB] + cM +=1 + cB +=1 + return merged_arr # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO + if(len(arr) <= 1): + return arr + else: + half = len(arr) // 2 + arr1 = arr[half:] + arr2 = arr[:half] + + sorted_1 = merge_sort(arr1) + sorted_2 = merge_sort(arr2) + + return merge(sorted_1, sorted_2) return arr @@ -17,12 +50,24 @@ def merge_sort( arr ): # STRETCH: implement an in-place merge sort algorithm def merge_in_place(arr, start, mid, end): # TO-DO - + for i in range(start, end): + for x in range(start, end): + if(arr[x] > arr[x+1]): + t = arr[x] + arr[x] = arr[x+1] + arr[x+1] = t return arr def merge_sort_in_place(arr, l, r): # TO-DO + m = (l+r)//2 + if(r <= l): + return arr + + merge_sort_in_place(arr, l, m) + merge_sort_in_place(arr, m+1, r) + merge_in_place(arr, l, m, r) return arr diff --git a/src/recursive_sorting/test_recursive.py b/src/recursive_sorting/test_recursive.py index 12d62395..adecf505 100644 --- a/src/recursive_sorting/test_recursive.py +++ b/src/recursive_sorting/test_recursive.py @@ -17,18 +17,18 @@ def test_merge_sort(self): self.assertEqual(merge_sort(arr5), sorted(arr5)) # Uncomment this test to test your in-place merge sort implementation - # def test_in_place_merge_sort(self): - # arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] - # arr2 = [] - # arr3 = [2] - # arr4 = [0, 1, 2, 3, 4, 5] - # arr5 = random.sample(range(200), 50) + def test_in_place_merge_sort(self): + arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] + arr2 = [] + arr3 = [2] + arr4 = [0, 1, 2, 3, 4, 5] + arr5 = random.sample(range(200), 50) - # self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9]) - # self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), []) - # self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2]) - # self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5]) - # self.assertEqual(merge_sort_in_place(arr5, 0, len(arr5)-1), sorted(arr5)) + self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9]) + self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), []) + self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2]) + self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5]) + self.assertEqual(merge_sort_in_place(arr5, 0, len(arr5)-1), sorted(arr5)) if __name__ == '__main__':