diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6a..ca24b350 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..e0122f85 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -5,26 +5,60 @@ def selection_sort( arr ): cur_index = i smallest_index = cur_index # TO-DO: find next smallest element - # (hint, can do in 3 loc) - - - + # (hint, can do in 3 loc) + for j in range(cur_index, len(arr)): + if arr[smallest_index] > arr[j]: + smallest_index = j # TO-DO: 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 ): - + swap = True + #While swap is True + while swap is True: + #Compare each element to its neighbor + swap = False + for i in range(len(arr) - 1): + #If elements in wrong position (relative to each other, swap them) + if arr[i] > arr[i+1]: + arr[i], arr[i+1] = arr[i+1], arr[i] + swap = True + return arr # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): - - return arr \ No newline at end of file + if len(arr) < 1: + return arr + + maximum = max(arr) + + if min(arr) < 0: + return "Error, negative numbers not allowed in Count Sort" + + count = [0 * i for i in range(maximum + 1)] + + for x in arr: + count[x] += 1 + + for i in range(len(count) - 1): + count[i+1] = count[i+1] + count[i] + + for i in range(len(count)): + count[i] -= 1 + + final = [0 * i for i in range(len(arr))] + + for x in arr: + fin_index = count[x] + count[x] -= 1 + final[fin_index] = x + + return final \ No newline at end of file diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..1bad137b 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,31 +1,88 @@ -# 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 + merged_arr = [] + + #While arrA is not empty and arrB is not empty + while len(arrA) >= 1 and len(arrB) >= 1: + if arrA[0] <= arrB[0]: + merged_arr.append(arrA[0]) + arrA = arrA[1:] + else: + merged_arr.append(arrB[0]) + arrB = arrB[1:] + + while len(arrA) >=1: + merged_arr.append(arrA[0]) + arrA = arrA[1:] + + while len(arrB) >=1: + merged_arr.append(arrB[0]) + arrB = arrB[1:] return merged_arr # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): - # TO-DO - - return arr + # Base Case if array is empty or length 1 + if len(arr) <= 1: + return arr + # Split arrays into half + arrA = [] + arrB = [] + for i , x in enumerate(arr): + if i < len(arr)/2: + arrA.append(x) + else: + arrB.append(x) + # Sort each half + arrA = merge_sort(arrA) + arrB = merge_sort(arrB) + # Merge back together + return merge(arrA, arrB) # STRETCH: implement an in-place merge sort algorithm def merge_in_place(arr, start, mid, end): - # TO-DO + # Maintain two pointers which point to start of the segments which have to be merged. + start2 = mid + 1 + # Compare the elements at which the pointers are present. + if arr[mid] <= arr[start2]: + return + + while start <= mid and start2 <= end: + # If element1 < element2 then element1 is at right position, simply increase pointer1. + if arr[start] <= arr[start2]: + start += 1 + # Else place element2 in its right position and all the elements at the right of element2 will be shifted right by one position. + else: + val = arr[start2] + i = start2 - return arr + while i != start: + arr[i] = arr[i - 1] + i -= 1 + + arr[start] = val -def merge_sort_in_place(arr, l, r): - # TO-DO + # Increment all the pointers by 1. + start += 1 + mid += 1 + start2 += 1 +def merge_sort_in_place(arr, l, r): + + if l < r: + + m = (l + (r - 1)) // 2 + + merge_sort_in_place(arr, l, m) + merge_sort_in_place(arr, m + 1, r) + + merge_in_place(arr, l, m, r) + return arr - # STRETCH: implement the Timsort function below # hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt def timsort( arr ):