diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..8b4caeb4 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)-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] + return arr +def bubble_sort( arr ): + 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]: + arr[i], arr[i+1] = arr[i+1], arr[i] + sorted_elems += 1 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 + cntArr = [0] * (max(arr)+1) + for e in arr: + cntArr[e]+=1 -# STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): + index = 0 + for i, e in enumerate(cntArr): + 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__': 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