diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6a..31db652f 100644 Binary files a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc and b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc differ diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..bb537c75 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,62 @@ +import numpy as np + + # 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) - - - + for j in range(i + 1, len(arr)): + if arr[j] < arr[smallest_index]: + smallest_index = j # TO-DO: swap - - - + if cur_index != smallest_index: + arr[cur_index], arr[smallest_index] = \ + arr[smallest_index], arr[cur_index] return arr # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): - + num_swaps = 1 + while num_swaps != 0: + num_swaps = 0 + for i in range(len(arr) - 1): + if arr[i] > arr[i + 1]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + num_swaps += 1 return arr # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): - - return arr \ No newline at end of file + #Find the maximum value: + if maximum == -1: + try: + maximum = max(arr) + except ValueError: + return [] + + # Store the count of each unique object. + count = np.zeros(maximum + 1) + for num in arr: + if num < 0: + return 'Error, negative numbers not allowed in Count Sort' + count[num] += 1 + + # Make the count array cumulative. + for i in range(1, len(count)): + count[i] += count[i - 1] + + output_arr = arr.copy() + for num in arr: + output_arr[int(count[num]) - 1] = num + count[num] -= 1 + + return output_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..07780c4b 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -1,6 +1,6 @@ import unittest import random -from iterative_sorting import * +from iterative_sorting import bubble_sort, count_sort, selection_sort class IterativeSortingTest(unittest.TestCase): def test_selection_sort(self): @@ -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..c5777bb6 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,16 +1,34 @@ -# 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 - + j, k = 0, 0 + for i in range(len(merged_arr)): + if k > len(arrB) - 1: + merged_arr[i] = arrA[j] + j += 1 + elif j > len(arrA) - 1: + merged_arr[i] = arrB[k] + k += 1 + elif arrA[j] < arrB[k]: + merged_arr[i] = arrA[j] + j += 1 + elif arrB[k] < arrA[j]: + merged_arr[i] = arrB[k] + k += 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: + arrA = arr[:len(arr) // 2] + arrB = arr[len(arr) // 2:] + return merge(merge_sort(arrA), merge_sort(arrB)) return arr diff --git a/src/recursive_sorting/test_recursive.py b/src/recursive_sorting/test_recursive.py index 12d62395..313cd2e1 100644 --- a/src/recursive_sorting/test_recursive.py +++ b/src/recursive_sorting/test_recursive.py @@ -1,8 +1,17 @@ import unittest import random -from recursive_sorting import * +from recursive_sorting import merge, merge_sort class RecursiveSortingTests(unittest.TestCase): + def test_merge(self): + arr1 = [1, 3, 5, 7, 9] + arr2 = [2, 4, 6] + arr3 = [] + arr4 = [1, 2, 3] + self.assertEqual(merge(arr1, arr2), [1, 2, 3, 4, 5, 6, 7, 9]) + self.assertEqual(merge(arr3, arr4), [1, 2, 3]) + self.assertEqual(merge(arr3, arr3), []) + def test_merge_sort(self): arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] arr2 = [] diff --git a/src/searching/searching.py b/src/searching/searching.py index d4969a0c..dba99444 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,30 +1,44 @@ # STRETCH: implement Linear Search def linear_search(arr, target): - - # TO-DO: add missing code - - return -1 # not found + for i in range(len(arr)): + if arr[i] == target: + return i + return -1 # not found # STRETCH: write an iterative implementation of Binary Search def binary_search(arr, target): - - if len(arr) == 0: - return -1 # array empty - low = 0 - high = len(arr)-1 - - # TO-DO: add missing code + if len(arr) == 0: + return -1 # array empty + + low = 0 + high = len(arr) + + # TO-DO: add missing code + while high > low: + mid = (low + high) // 2 + if arr[mid] == target: + return mid + elif arr[mid] > target: + high = mid + elif arr[mid] < target: + low = mid + 1 + - return -1 # not found + return -1 # not found # STRETCH: write a recursive implementation of Binary Search def binary_search_recursive(arr, target, low, high): - middle = (low+high)//2 - - if len(arr) == 0: - return -1 # array empty - # TO-DO: add missing if/else statements, recursive calls + mid = (low + high) // 2 + if len(arr) == 0: + return -1 # array empty + # TO-DO: add missing if/else statements, recursive calls + if arr[mid] == target: + return mid + elif arr[mid] > target: + return binary_search_recursive(arr, target, low, mid) + else: + return binary_search_recursive(arr, target, mid + 1, high) diff --git a/src/searching/test_searching.py b/src/searching/test_searching.py index 7d415fac..c8e40c43 100644 --- a/src/searching/test_searching.py +++ b/src/searching/test_searching.py @@ -1,5 +1,5 @@ import unittest -from searching import * +from searching import binary_search, binary_search_recursive, linear_search class SearchingTests(unittest.TestCase): @@ -16,8 +16,11 @@ def test_binary_search(self): arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9] arr2 = [] + self.assertEqual(binary_search(arr1, -9), 0) self.assertEqual(binary_search(arr1, -8), 1) self.assertEqual(binary_search(arr1, 0), 6) + self.assertEqual(binary_search(arr1, 9), 13) + self.assertEqual(binary_search(arr2, 6), -1) self.assertEqual(binary_search(arr2, 0), -1)