From 81270e2a2d4285adb4126989077b0331f4ee93bd Mon Sep 17 00:00:00 2001 From: Baisal89 Date: Tue, 17 Mar 2020 16:35:36 -0400 Subject: [PATCH 1/2] iterative and resursive sorting --- src/iterative_sorting/iterative_sorting.py | 48 ++++++++++++++-------- src/recursive_sorting/recursive_sorting.py | 32 +++++++++++---- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..7e11dbfe 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,42 @@ -# TO-DO: Complete the selection_sort() function below +# 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) - - - - - # TO-DO: swap - - - - + cur_index = i#establishes first index + for j in range(i+1, len(arr)):#the array besides the first + if arr[cur_index] > arr[j]:# if the first is greater than anything + cur_index = j#new starting point + arr[i], arr[cur_index] = arr[cur_index], arr[i] return arr - +list = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): - + swapped = True + while swapped: + swapped = False + for i in range(len(arr)-1): + if arr[i] > arr[i+1]: + arr[i],arr[i+1] = arr[i+1],arr[i] + swapped = True return arr + + # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): - - return arr \ No newline at end of file + # first is to tally all of the occurances of each number and make an array that is ordered by index of all numbers 0-9 + # second is to add the tally number to the next number in the line + count_index = [0,0,0,0,0,0,0,0,0,0] + for i in range(len(arr)):#tally occurances + count_index[arr[i]] += 1 + for i in range(len(count_index)):#add the previous number to the next number + count_index[i] = count_index[i]+count_index[i-1] + count_index.pop() + count_index.insert(0,0)#shift all numbers to the right + output_arr = [0]*len(arr) + for i in range(len(arr)):#populate output array + output_arr[count_index[arr[i]]] = arr[i] + count_index[arr[i]]+=1 + return output_arr diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..a5039537 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,17 +1,32 @@ # TO-DO: complete the helpe function below to merge 2 sorted arrays def merge( arrA, arrB ): - elements = len( arrA ) + len( arrB ) - merged_arr = [0] * elements - # TO-DO - - return merged_arr + result = [] + i = j = 0 + while i < len(arrA) and j < len(arrB): + if arrA[i] < arrB[j]: + result.append(arrA[i]) + i+=1 + else: + result.append(arrB[j]) + j+=1 + result += arrA[i:] + result += arrB[j:] + + + return result # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): - # TO-DO + if len(arr) < 2: + return arr + mid = len(arr)//2 + + left=merge_sort(arr[:mid]) + right=merge_sort(arr[mid:]) + + return merge(left,right) - return arr # STRETCH: implement an in-place merge sort algorithm @@ -20,14 +35,13 @@ 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 # STRETCH: implement the Timsort function below -# hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt def timsort( arr ): return arr From 757b45b56affb4bd344f6abc4619d0f341a8e209 Mon Sep 17 00:00:00 2001 From: Baisal89 Date: Wed, 15 Apr 2020 16:43:06 -0400 Subject: [PATCH 2/2] assignment 1 part 2 --- src/recursive_sorting/rec.py | 7 ++++ src/recursive_sorting/recursive_sorting.py | 42 +++++++++++++++------- src/recursive_sorting/test_recursive.py | 2 +- 3 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 src/recursive_sorting/rec.py diff --git a/src/recursive_sorting/rec.py b/src/recursive_sorting/rec.py new file mode 100644 index 00000000..d9967d1f --- /dev/null +++ b/src/recursive_sorting/rec.py @@ -0,0 +1,7 @@ + +def greet(n): + if n == 1: + return 1 + return n * greet(n - 1) + + diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index a5039537..c024986b 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,4 +1,5 @@ # TO-DO: complete the helpe function below to merge 2 sorted arrays + def merge( arrA, arrB ): result = [] i = j = 0 @@ -15,33 +16,48 @@ def merge( arrA, arrB ): return result +#arrA = [2,3,4,5,6,7] +#arrB = [9,10,11,12,13] +#merge(arrA, arrB) # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): - if len(arr) < 2: + #(base case) if the array is empty or lenght 1 return + if len(arr) <= 1: return arr - mid = len(arr)//2 + left = merge_sort(arr[:len(arr)//2]) + right = merge_sort(arr[len(arr)//2:]) + arr = merge(left, right) - left=merge_sort(arr[:mid]) - right=merge_sort(arr[mid:]) + return arr +arr = [2,5,3,6,9,4,15,0] +merge_sort(arr) + # TO-DO - return merge(left,right) +# TO-DO: implement the Merge Sort function below USING RECURSION +def merge_sort( arr ): + #split here + #find the middle of arr + #put stuff to the left in left + #put the stuff to the right in right -# STRETCH: implement an in-place merge sort algorithm -def merge_in_place(arr, start, mid, end): + #merge left and right + +# # STRETCH: implement an in-place merge sort algorithm +#def merge_in_place(arr, start, mid, end): # TO-DO - return arr + # return arr -def merge_sort_in_place(arr, l, r): +#def merge_sort_in_place(arr, l, r): # TO-DO - return arr + #return arr -# STRETCH: implement the Timsort function below -def timsort( arr ): +# # STRETCH: implement the Timsort function below +#def timsort( arr ): - return arr + #return arr \ No newline at end of file diff --git a/src/recursive_sorting/test_recursive.py b/src/recursive_sorting/test_recursive.py index 12d62395..fffeea46 100644 --- a/src/recursive_sorting/test_recursive.py +++ b/src/recursive_sorting/test_recursive.py @@ -32,4 +32,4 @@ def test_merge_sort(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()