From 9243cbed9f87332671f44989f7a23ee9110dcc0f Mon Sep 17 00:00:00 2001 From: Dorothy Gasque Date: Mon, 13 Apr 2020 16:05:52 -0700 Subject: [PATCH 1/3] update with completion of iterative_sorting.py --- .../iterative_sorting.cpython-37.pyc | Bin 815 -> 1639 bytes src/iterative_sorting/iterative_sorting.py | 70 +++++++++++++++--- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6aa9951159b6a50bac8891d62dd4090bdc..ca24b350c43a7e125630e25357ccbf1c5c47129e 100644 GIT binary patch literal 1639 zcmbtU&ube;6rMM;yOI^fb%L;EC`OcCRDy9KG@%GnV)xg@hdL!7G0{rP)=Y zhFvJwr=HSMa%%s8{x?1EwI`o?&!zp|jBKZAZyhmjp5Dxx_vU-=+ef!=Hw4BHfByW< zuXQ2*;6GPGLHPtT+kwzR(}m~@Ew#c*x~_d4Tu@)?+Og=ajR}W%1eiYN0cJLXSf1wa z&8ZOQq-9&2%R_PUxTJzgS;$f?KgdNP-M-yxmtG+XwJjjMZE@zAy}~QieD4WcwVc1h5gqP^A_(T7j<-ru;P+4>dMMD!()CV| z$6=Pc{}r89H2h7NHTb{&>fqzS*I8(?fu5X($>1;@FQ#FVXM@k9{ENkCaGaVvN+yHM zj0aI3nqeM23!i2y>%T0!v+t_)ER4f(9;JyrClM!WK?vCEnC~5MEVbGa7Sw0XX2D)r0W5>b zpmGVU6*|@j0>x5xRrB_hW!?tlup+`s?UghSIG=g2DujRZ*l-aHzwomz8BV<$7;cZg zmpvcO40lWAkJCkx8}y&3Xk>ABf_t;NHCl{Dv4eCIhU`5EL7r?#fJJu!7enP{LpA|i zfN9oo>hdp)F^A{D0j^u~5i(n0M96=GTgNqFNx5X~xIMMR-aW03h4!?6A#%l7Z(;Y4 zPpI50)H#vJJ=_J&>4jaF?-l~B-U7m^@O+@I>48uJt*W2E!+9?6=gRqN0y%K+Ne6XM zh0k$70%+S}%dTvJ_p3L&-@o##S zbtg(X2VCzCGv~UqxklU`$C#E36ZG#%vm`AFYKb4^+(R^S{lv@!v*qrdZJ@V0=5?bN zM_E2jr?dSYU&nU`;>C__GfTPMUYw4HakdZT>gfMT#cUwNf8}ECVB^c1B)YyOL8Yd{ zB}9}M{>oPV%tT4vtvhPeuo+Dk(|!#HSTRjLM9URjzKxP$++V}uzF2R)ziQk(^gf4X zAwUriB;lVYH?QbIOrA~ZK;B*H4e0p(&^nnd_#FZCcd&Mq{S;YR*^M`%ay^VCYrQv3 R^&$@U8S{(_T5r{N{suPfPWb=; literal 815 zcmZXS&2G~`5P)ZPoyKXCDu)Oq9C|=p%mMenfkT8U;D~}mi;yf(alA`o>YsMjK|yj( z?E~-{JO%IJE2lgF7cMZn4lRPQc6MiGcW35XKOGEqf#uuLkGEeWz;|o>5w_uohdE%S zK;bn+poEe-{?#yRazEqpWq~B z$O^<5J@n)Vhv?&hPxei2fO>3Ls1E1{Uaa2V0(HP<3=?Xc00Tt^`lyyloO^Uq3-(Ke zV{jNxZY{f-y4Yi2wD%aE+shz_Hz3JYNG(XRe*@WC0*`Dtw8JX$DxF_Y*V>j@t9ra@ z_U6m^JZp)J+0k~2Bks_{yWDA?KicES<`+w1b6K&oP3hXT`P{4G%v~J_mpkg_@hZ)i z`M*hblX6*9j_!RvYTTuQ2E3jHNPV6I>F~PDF7VeKt_4@UKg~ arr[j]: + smallest_index = j # TO-DO: swap - - - - + a = arr[cur_index] + arr[cur_index] = arr[smallest_index] + arr[smallest_index] = a + 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 + i = 0 + count = 0 + while i < (len(arr) - 1): + + #If elements in wrong position (relative to each other, swap them) + if arr[i] > arr[i+1]: + a = arr[i] + b = arr[i+1] + arr[i] = b + arr[i+1] = a + i += 1 + count += 1 + else: + i += 1 + # If no swaps performed, stop. + if count < 1: + swap = False + # Else, go back to the element at index 0 and repeat step 1. + else: + 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) + minimum = min(arr) + + if minimum < 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 From 2f570266fa6b643f50a09fa379caa799e904ac30 Mon Sep 17 00:00:00 2001 From: Dorothy Gasque Date: Mon, 13 Apr 2020 16:16:06 -0700 Subject: [PATCH 2/3] Update iterative_sorting.py --- src/iterative_sorting/iterative_sorting.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index f609699f..83a438f9 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -54,9 +54,8 @@ def count_sort( arr, maximum=-1 ): return arr maximum = max(arr) - minimum = min(arr) - if minimum < 0: + if min(arr) < 0: return "Error, negative numbers not allowed in Count Sort" count = [0 * i for i in range(maximum + 1)] From 478b35ecd7dce78b98ea76769c9ca921d279868c Mon Sep 17 00:00:00 2001 From: Dorothy Gasque Date: Tue, 14 Apr 2020 14:43:32 -0700 Subject: [PATCH 3/3] update recursive_sorting.py with solutions to merge_sort and merge_sort_in_place also includes some adjustments to iterative_sorting --- src/iterative_sorting/iterative_sorting.py | 29 ++------ src/recursive_sorting/recursive_sorting.py | 81 ++++++++++++++++++---- 2 files changed, 76 insertions(+), 34 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 83a438f9..e0122f85 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -11,9 +11,8 @@ def selection_sort( arr ): smallest_index = j # TO-DO: swap - a = arr[cur_index] - arr[cur_index] = arr[smallest_index] - arr[smallest_index] = a + arr[smallest_index], arr[cur_index] = arr[cur_index], arr[smallest_index] + return arr @@ -24,27 +23,13 @@ def bubble_sort( arr ): #While swap is True while swap is True: #Compare each element to its neighbor - i = 0 - count = 0 - while i < (len(arr) - 1): - + 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]: - a = arr[i] - b = arr[i+1] - arr[i] = b - arr[i+1] = a - i += 1 - count += 1 - else: - i += 1 - - # If no swaps performed, stop. - if count < 1: - swap = False - # Else, go back to the element at index 0 and repeat step 1. - else: - swap = True + arr[i], arr[i+1] = arr[i+1], arr[i] + swap = True + return arr 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 ):