diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..0be893a5 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,49 @@ # 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 - - - - - return arr +# # loop through n-1 elements +# # len(arr) => 5 +# # out of bounds +# # len(arr) - 1 +# # 4 -> 3 + 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) +# # iterate over a range + for j in range(cur_index, len(arr)): + if arr[j] < arr[smallest_index]: + smallest_index = j + + +# # swap + arr[smallest_index], arr[cur_index] = arr[cur_index], arr[smallest_index] + + return arr # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): + n = len(arr) + + for i in range(n): + for j in range(n-i-1): + if arr[j] > arr[j+1] : + arr[j], arr[j+1] = arr[j+1], arr[j] ## keep swapping the order and change the position ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ) an so on return arr # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): + m = maximum = 0 + count = [0] * m + for a in arr: + count[a] += 1 + i = 0 + for a in range(m): + for c in range(count[a]): + arr[i] = a + i += 1 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..43615fc6 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..fcb2f9c3 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -3,14 +3,38 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO - + L = 0 + R = 0 + for i in range(0, elements): + if L >= len(arrA): + merged_arr[i] = arrB[R] + R += 1 + elif R >= len(arrB): + merged_arr[i] = arrA[L] + L += 1 + elif arrA[L] < arrB[R]: + merged_arr[i] = arrA[L] + L += 1 + else: + merged_arr[i] = arrB[R] + R += 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] + + ms_1 = merge_sort(arr1) + ms_2 = merge_sort(arr2) + return merge(ms_1, ms_2) return arr diff --git a/src/searching/searching.py b/src/searching/searching.py index d4969a0c..ee692c03 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -3,6 +3,10 @@ def linear_search(arr, target): # TO-DO: add missing code + for value in arr: + if value == target: + return 1 + return -1 # not found diff --git a/src/searching/test_searching.py b/src/searching/test_searching.py index 7d415fac..d141613b 100644 --- a/src/searching/test_searching.py +++ b/src/searching/test_searching.py @@ -1,4 +1,4 @@ -import unittest +import unittest from searching import *