From 2a9427ee30d39e9d4f757144ed1eeb1a35d9fa82 Mon Sep 17 00:00:00 2001 From: Cireimu Date: Mon, 13 Apr 2020 15:24:51 -0700 Subject: [PATCH 1/2] day one mvp and some stretch done --- src/iterative_sorting/iterative_sorting.py | 31 ++++++++------- src/searching/searching.py | 45 ++++++++++++++-------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..675cd905 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,35 @@ -# 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(0, len(arr) - 1-i): + if arr[j] > arr[j+1]: + temp = arr[j] + arr[j] = arr[j+1] + arr[j+1] = temp 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/searching/searching.py b/src/searching/searching.py index d4969a0c..3cc51ea6 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,30 +1,41 @@ -# 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 - return -1 # not found + # 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 -# 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 From 2744622599fb671705c3567ffb6e4054106246a2 Mon Sep 17 00:00:00 2001 From: Cireimu Date: Tue, 14 Apr 2020 18:30:52 -0700 Subject: [PATCH 2/2] day two mvp with one more stretch --- src/iterative_sorting/iterative_sorting.py | 8 ++-- src/recursive_sorting/recursive_sorting.py | 46 +++++++++++++++++++--- src/searching/searching.py | 9 +++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 675cd905..b5b042da 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -21,11 +21,9 @@ def selection_sort(arr): # TO-DO: implement the Bubble Sort function below def bubble_sort(arr): for i in range(0, len(arr) - 1): - for j in range(0, len(arr) - 1-i): - if arr[j] > arr[j+1]: - temp = arr[j] - arr[j] = arr[j+1] - arr[j+1] = temp + for j in range(i+1, len(arr)): + if arr[i] > arr[j]: + arr[i], arr[j] = arr[j], arr[i] 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 3cc51ea6..b0125dcc 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -39,3 +39,12 @@ def binary_search_recursive(arr, target, low, high): 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