diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 6e14b383..cb3a76ba 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,25 +1,59 @@ # TO-DO: Complete the selection_sort() function below def selection_sort(arr): # loop through n-1 elements + print("Starting sort.") 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) # Your code here - - + for n in range(i, len(arr)): + if arr[n] < arr[smallest_index]: + smallest_index = n # TO-DO: swap # Your code here + smallest = arr[smallest_index] + del arr[smallest_index] + arr.insert(i, smallest) + print(f"Moved {smallest} to index: {smallest_index}") + print("Finished sorting list.") + print(f"{arr}") return arr # TO-DO: implement the Bubble Sort function below def bubble_sort(arr): # Your code here + finished = False + sorts_count = 0 + arr_range_max = len(arr) - 1 + + print("Starting sort.") + while finished is not True: + for i in range(0, arr_range_max): + left_index = i + right_index = i + 1 + lhs = arr[left_index] + rhs = arr[right_index] + print(f"left: {lhs} right: {rhs}") + if lhs > rhs: + del arr[right_index] + arr.insert(left_index, rhs) + sorts_count += 1 + + if right_index == arr_range_max: + if sorts_count == 0: + print(f"Finished sorting list.") + finished = True + else: + print(f"Sorted {sorts_count} times. Re-starting.") + sorts_count = 0 + continue + print(f"{arr}") return arr ''' @@ -41,6 +75,25 @@ def bubble_sort(arr): ''' def counting_sort(arr, maximum=None): # Your code here + if len(arr) == 0: + return arr + if maximum is None: + maximum = max(arr) + buckets = [0 for i in range(maximum + 1)] - return arr + for value in arr: + if value < 0: + return "Error negative numbers not allowed for count sort." + buckets[value] += 1 + + output = [] + + for index, count in enumerate(buckets): + output.extend([index for i in range(count)]) + + return output + +#my_arr = [23, 8, 42, 4, 16, 15] +#bubble_sort(my_arr) +#selection_sort(my_arr) \ No newline at end of file diff --git a/src/iterative_sorting/test_iterative.py b/src/iterative_sorting/test_iterative.py index 24f1b0bb..a4f289cf 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -26,17 +26,17 @@ 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) + 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(counting_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) - # self.assertEqual(counting_sort(arr2), []) - # self.assertEqual(counting_sort(arr3), "Error, negative numbers not allowed in Count Sort") - # self.assertEqual(counting_sort(arr4), sorted(arr4)) + self.assertEqual(counting_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) + self.assertEqual(counting_sort(arr2), []) + self.assertEqual(counting_sort(arr3), "Error, negative numbers not allowed in Count Sort") + self.assertEqual(counting_sort(arr4), sorted(arr4)) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file diff --git a/src/searching/searching.py b/src/searching/searching.py index 9c7ac8ee..3e1fb672 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,14 +1,28 @@ def linear_search(arr, target): # Your code here - + for x in range(0, len(arr)): + if target == arr[x]: + print(f"target found in list at index {x}.") + return x return -1 # not found # Write an iterative implementation of Binary Search -def binary_search(arr, target): +def binary_search(arr, target, start = None, end = None): + if start is None and end is None: + start = 0 + end = len(arr) - 1 # Your code here + if start > end: + return -1 + else: + mid = (start + end) // 2 - - return -1 # not found + if arr[mid] == target: + return mid + elif arr[mid] > target: + return binary_search(arr, target, start, mid - 1) + else: + return binary_search(arr, target, mid + 1, end) \ No newline at end of file