diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6a..6ca85c86 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..ee4b350d 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -2,15 +2,19 @@ def selection_sort( arr ): # loop through n-1 elements for i in range(0, len(arr) - 1): - cur_index = i + cur_index = i # smallest_index = cur_index - # TO-DO: find next smallest element + # TO-DO: find next smallest element, (x, y = y, x) # (hint, can do in 3 loc) - - + for j in range(cur_index, len(arr)): + if arr[j] < arr[smallest_index]: + smallest_index = j # TO-DO: swap + temp = arr[smallest_index] + arr[smallest_index] = arr[cur_index] + arr[cur_index] = temp @@ -19,12 +23,17 @@ def selection_sort( arr ): # TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): - +def bubble_sort(arr): + n = len(arr) + for i in range(n - 1): + # For each element in the array... + for j in range(n - 1 - i): + # Check it's neighbor to the right... + if arr[j] > arr[j + 1]: + # If neighbor is smaller, swap and make Flag true + arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr - # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): - return arr \ No newline at end of file diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..d4a97f74 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,15 +1,54 @@ # TO-DO: complete the helpe function below to merge 2 sorted arrays +# We are only comparing the first elements of the arrays, as they are already sorted def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO - + # Initialise pointers for the front of Arrays A & B + a = 0 + b = 0 + for i in range(0, elements): + # compare thearrays + if a >= len(arrA): # all elements in arrA have been merged + merged_arr[i] = arrB[b] + b += 1 + elif b >= len(arrB): # all elements in arrB have been merged + merged_arr[i] = arrA[a] + a += 1 + elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array + merged_arr[i] = arrA[a] + a += 1 + else: # else, next element in arrB must be smaller, so add it to final array + merged_arr[i] = arrB[b] + b += 1 return merged_arr +def merge(arrA, arrB): + elements = len(arrA) + len(arrB) + merged_arr = [0] * elements + a = 0 + b = 0 + # since arrA and arrB already sorted, we only need to compare the first element of each when merging! + for i in range(0, elements): + + + # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO + def merge_sort( arr ): + if len(arr) > 1: + # split the array + mid = len(arr) // 2 + # split again - left + left = arr[:mid] + # split again - right + right = arr[mid:] + arr = merge(left, right) + + mergeSort(left) + mergeSort(right) return arr