diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..b5b042da 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,33 @@ -# TO-DO: Complete the selection_sort() function below -def selection_sort( arr ): +# 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) - - - + # (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 - - - + temp = arr[cur_index] + arr[cur_index] = arr[smallest_index] + arr[smallest_index] = temp return arr # TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): - +def bubble_sort(arr): + for i in range(0, len(arr) - 1): + for j in range(i+1, len(arr)): + if arr[i] > arr[j]: + arr[i], arr[j] = arr[j], arr[i] return arr # STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): +def count_sort(arr, maximum=-1): - return arr \ No newline at end of file + return arr diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..7257203c 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,16 +1,49 @@ # TO-DO: complete the helpe function below to merge 2 sorted arrays -def merge( arrA, arrB ): - elements = len( arrA ) + len( arrB ) +import math + + +def merge(arrA, arrB): + elements = len(arrA) + len(arrB) merged_arr = [0] * elements # TO-DO - + i = 0 + j = 0 + merged_index = 0 + while i < len(arrA) and j < len(arrB): + if arrA[i] > arrB[j]: + merged_arr[merged_index] = arrB[j] + j += 1 + + elif arrA[i] < arrB[j]: + merged_arr[merged_index] = arrA[i] + i += 1 + + merged_index += 1 + + merged_arr = merged_arr[:merged_index] + if i < len(arrA): + merged_arr.extend(arrA[i:]) + if j < len(arrB): + merged_arr.extend(arrB[j:]) + return merged_arr # TO-DO: implement the Merge Sort function below USING RECURSION -def merge_sort( arr ): + + +def merge_sort(arr): # TO-DO + if len(arr) <= 1: + return arr + mid_index = math.floor(len(arr)/2) + left = arr[:mid_index] + right = arr[mid_index:] + left_sorted = merge_sort(left) + right_sorted = merge_sort(right) + + arr = merge(left_sorted, right_sorted) return arr @@ -20,7 +53,8 @@ def merge_in_place(arr, start, mid, end): return arr -def merge_sort_in_place(arr, l, r): + +def merge_sort_in_place(arr, l, r): # TO-DO return arr @@ -28,6 +62,6 @@ def merge_sort_in_place(arr, l, r): # STRETCH: implement the Timsort function below # hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt -def timsort( arr ): +def timsort(arr): return arr diff --git a/src/searching/searching.py b/src/searching/searching.py index d4969a0c..b0125dcc 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,30 +1,50 @@ -# STRETCH: implement Linear Search +# STRETCH: implement Linear Search def linear_search(arr, target): - + # TO-DO: add missing code + for i in range(len(arr)): + if arr[i] == target: + return i - return -1 # not found + return -1 # not found -# STRETCH: write an iterative implementation of Binary Search +# 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 + if len(arr) == 0: + return -1 # array empty - # TO-DO: add missing code + low = 0 + high = len(arr)-1 + + # TO-DO: add missing code + while low <= high: + middle = (low+high)//2 + if target < arr[middle]: + high = middle-1 + elif target > arr[middle]: + low = middle+1 + else: + return middle - return -1 # not found + return -1 # not found -# STRETCH: write a recursive implementation of Binary Search +# 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 + middle = (low+high)//2 + + if len(arr) == 0: + return -1 # array empty + # TO-DO: add missing if/else statements, recursive calls + while low <= high: + if arr[middle] == target: + return middle + elif target < arr[middle]: + high = middle-1 + else: + low = middle+1 + return binary_search_recursive(arr, target, low, high) + return -1