diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6a..a39d3bff 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..d5319bdb 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -4,26 +4,26 @@ def selection_sort( arr ): 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 - - - - + min_val = min(arr[cur_index+1: ]) + min_index = arr.index(min_val) + if min_val < arr[cur_index]: + arr[min_index]= arr[cur_index] + arr[cur_index] = min_val return arr # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): - + while arr != sorted(arr): + for i in range(0, len(arr) - 1): + left = arr[i] + right = arr[i+1] + if left > right: + arr[i], arr[i+1] = right, left return arr + # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..ebeba2c3 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,16 +1,35 @@ -# 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 + elem = len( arrA ) + len( arrB ) + merged_arr = [0] * elem # TO-DO - + while ((len(arrA) > 0) | (len(arrB) > 0)): + if not arrA: + merged_arr.append(arrB[0]) + arrB.pop(0) + elif not arrB: + merged_arr.append(arrA[0]) + arrA.pop(0) + elif (arrA[0] < arrB[0]): + merged_arr.append(arrA[0]) + arrA.pop(0) + elif (arrB[0] < arrA[0]): + merged_arr.append(arrB[0]) + arrB.pop(0) + merged_arr.pop(0) + return merged_arr # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO - + if len(arr) > 1: + mid = len(arr) // 2 + left = arr[0:mid] + right = arr[mid: ] + arr = merge(merge_sort(left), merge_sort(right)) + return arr