From 728dc3b13d4cb8165c7ba73fb6dfcab7a0ce15b8 Mon Sep 17 00:00:00 2001 From: nishaarya Date: Tue, 14 Apr 2020 11:41:20 +0100 Subject: [PATCH 1/6] part 1 --- .../iterative_sorting.cpython-37.pyc | Bin 815 -> 699 bytes src/iterative_sorting/iterative_sorting.py | 7 ++++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6aa9951159b6a50bac8891d62dd4090bdc..19cc2905a445ad8519e003e87afa733ffb0b0e67 100644 GIT binary patch delta 291 zcmZ3_wwsmLiI`C7XXQhtP+M4#uCOFhGxbTCJBaS zCKra-kcm|`BDJhF3^h!W3_z0EhM@+`n|MNvk!|98wdg38lGNOSl?+83Kr?57nNk@rNjGumBk~48aVVjDBEOX)+eEPWE7u=avRa6f*${2BygkOwo*rlkYGEFiUc<0RZ^h BLF)hj delta 434 zcmYL_y-ve05XbNAv;=6R3RNg&U_huSQx*n>4hSkHR1gIr@gc|;q-vW|JD>_V>d+S; z>QgZB01WIr0&l^Ka=*b}N_q%SZFVc2$>q-&0?KBcz+J15BK7GO3SXz&V_(Zp3#XyO3% z4h@6i5BP?}@cJKk3fs&GUSSn7rZD5fO~pgjF~qTA8ahN9s?Y(_e@a(%icyGLknzFV zDS4bV2W6_E1rBctM$ZWi;xNH=tzjHPXdY3dbY`Sc?@r8Vtv_Uz=BfHv^zVJYJF{z3 cLr$$?g)ADlLRL#OCna`{>ywxr@@2F11FX$iC;$Ke diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..4637e877 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -6,11 +6,16 @@ def selection_sort( arr ): smallest_index = cur_index # TO-DO: find next smallest element # (hint, can do in 3 loc) - + for j in range(cur_index, len(arr)): + if arr[j] < arr[smallest_index]: + smallest_index = j # TO-DO: swap + temp = arr[smallest_index] + arr[smallest_index] = arr[cur_index] + arr[cur_index] = temp From 307cfcc2ecf425b3444454c6793ae1e2101ab18b Mon Sep 17 00:00:00 2001 From: nishaarya Date: Tue, 14 Apr 2020 15:19:17 +0100 Subject: [PATCH 2/6] part 1 --- .../iterative_sorting.cpython-37.pyc | Bin 699 -> 865 bytes src/iterative_sorting/iterative_sorting.py | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 19cc2905a445ad8519e003e87afa733ffb0b0e67..44924e8dfafcff3ba8aecb4449fe3458d83e6a66 100644 GIT binary patch delta 423 zcmZ8du};H447D$pkfuqLp;B8WSSqDIz`_I_n7S}jssyNMI#e_?byDb%s6$7_h!0?7 zWM=0hy74F20Rh62-`PIj^EMzFF*OmPXJF(k#~NrZ)p42eZts7dsV&Fo>4 zW@Jg-7HIr{{CnSw)={*W-DFG354n)F7yz|$P-926(6KS#1q4WL<*WW&-t(ZE{2-ZZ w=E*wwPwGsU>$|+>>b=AK>z>% delta 274 zcmaFJwwsmDiIlA2qvlA(xWvLa)55-SHI4VUwGmQks)$2XX?~{VW`k9BcrMf-H6b diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 4637e877..3440efae 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -2,7 +2,7 @@ def selection_sort( arr ): # loop through n-1 elements for i in range(0, len(arr) - 1): - cur_index = i + cur_index = i # smallest_index = cur_index # TO-DO: find next smallest element # (hint, can do in 3 loc) @@ -11,7 +11,6 @@ def selection_sort( arr ): smallest_index = j - # TO-DO: swap temp = arr[smallest_index] arr[smallest_index] = arr[cur_index] @@ -25,11 +24,20 @@ def selection_sort( arr ): # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): - + swaps = 1 + + while swaps > 0: + swaps = 0 + for i in range(0, len(arr)-1): + # Swap if wrong positions + if arr[i] > arr[i+1]: + temp = arr[i] + arr[i] = arr[i+1] + arr[i+1] = temp + swaps += 1 return arr # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): - return arr \ No newline at end of file From dbdbc2b35a6ae2cd96b7c49fa23d83db36b7a2db Mon Sep 17 00:00:00 2001 From: nishaarya Date: Tue, 14 Apr 2020 19:54:51 +0100 Subject: [PATCH 3/6] part1 --- .../iterative_sorting.cpython-37.pyc | Bin 865 -> 844 bytes src/iterative_sorting/iterative_sorting.py | 24 ++++++++---------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 44924e8dfafcff3ba8aecb4449fe3458d83e6a66..6ca85c861c88a35de458cbd00c82703a5a464543 100644 GIT binary patch delta 386 zcmaFJc7~1DiI|%LJCUfvCy^DzjlI)Cp%O6blCm1v6+e`4uq%t=42J0y(FM8N_BO zVg*vS81sri&MRUE5gc%W6C}r-RGO5OlNw)~UsNIm6e#8a5)6zSj6zIeOd?ELlQ%Ny z8i4cw4F(w&#h#m3k(pbXi*68Ca(-!E3D_L9$$ZRGmY^Ui0-09C2O<#mf>~h8Ic#$C TQ%ZAE?LaOs2DzIGZYmP$eFED-~AVCuq9sS=>3&VWcr>Lk!1QHFj(#K;FQ z@)JyaK{x({yC4K;SDm;J@(Eafb zR~f%7S3Z*iLX}Q+m8 0: - swaps = 0 - for i in range(0, len(arr)-1): - # Swap if wrong positions - if arr[i] > arr[i+1]: - temp = arr[i] - arr[i] = arr[i+1] - arr[i+1] = temp - swaps += 1 +def bubble_sort(arr): + n = len(arr) + for i in range(n - 1): + # For each element in the array... + for j in range(n - 1 - i): + # Check it's neighbor to the right... + if arr[j] > arr[j + 1]: + # If neighbor is smaller, swap and make Flag true + arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr - # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): return arr \ No newline at end of file From 48339e7959ed8827558659bde3eb132b24cba73d Mon Sep 17 00:00:00 2001 From: nishaarya Date: Tue, 14 Apr 2020 22:22:48 +0100 Subject: [PATCH 4/6] part2 --- src/recursive_sorting/recursive_sorting.py | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..5888d06e 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -3,13 +3,58 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO + a = 0 + b = 0 + + return merged_arr + +# EXAMPLE: +def merge( arrA, arrB ): + # TO-DO + i=0 + j=0 + lenA = len(arrA) + lenB = len(arrB) + + arr = [] + + while((i < lenA) and (j < lenB)): + if(arrA[i] < arrB[j]): + arr.append(arrA[i]) + i = i + 1 + else: + arr.append(arrB[j]) + j = j + 1 + + while(i < lenA): + arr.append(arrA[i]) + i = i + 1 + + while(j < lenB): + arr.append(arrB[j]) + j = j + 1 return merged_arr + +arrA = [1, 4, 7, 9] +arrB = [10, 14, 15] # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO + def merge_sort( arr ): + if len(arr) > 1: + # split the array + mid = len(arr) // 2 + # split again - left + left = arr[:mid] + # split again - right + right = arr[mid:] + arr = merge(left, right) + + mergeSort(left) + mergeSort(right) return arr From f040fea2a61784984b49c9229efade3efe32e8b2 Mon Sep 17 00:00:00 2001 From: nishaarya Date: Wed, 15 Apr 2020 21:29:10 +0100 Subject: [PATCH 5/6] part2 --- src/recursive_sorting/recursive_sorting.py | 65 ++++++++++------------ 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index 5888d06e..3c09cd7a 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 +# We are only comparing the first elements of the arrays, as they are already sorted def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements @@ -8,50 +9,40 @@ def merge( arrA, arrB ): return merged_arr -# EXAMPLE: -def merge( arrA, arrB ): - # TO-DO - i=0 - j=0 - lenA = len(arrA) - lenB = len(arrB) - - arr = [] - - while((i < lenA) and (j < lenB)): - if(arrA[i] < arrB[j]): - arr.append(arrA[i]) - i = i + 1 - else: - arr.append(arrB[j]) - j = j + 1 - - while(i < lenA): - arr.append(arrA[i]) - i = i + 1 - - while(j < lenB): - arr.append(arrB[j]) - j = j + 1 - +def merge(arrA, arrB): + elements = len(arrA) + len(arrB) + merged_arr = [0] * elements + a = 0 + b = 0 + # since arrA and arrB already sorted, we only need to compare the first element of each when merging! + for i in range(0, elements): + if a >= len(arrA): # all elements in arrA have been merged + merged_arr[i] = arrB[b] + b += 1 + elif b >= len(arrB): # all elements in arrB have been merged + merged_arr[i] = arrA[a] + a += 1 + elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array + merged_arr[i] = arrA[a] + a += 1 + else: # else, next element in arrB must be smaller, so add it to final array + merged_arr[i] = arrB[b] + b += 1 return merged_arr - -arrA = [1, 4, 7, 9] -arrB = [10, 14, 15] # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO def merge_sort( arr ): - if len(arr) > 1: - # split the array - mid = len(arr) // 2 - # split again - left - left = arr[:mid] - # split again - right - right = arr[mid:] - arr = merge(left, right) + if len(arr) > 1: + # split the array + mid = len(arr) // 2 + # split again - left + left = arr[:mid] + # split again - right + right = arr[mid:] + arr = merge(left, right) mergeSort(left) mergeSort(right) From c8b898dfc17fd9e9e426ffab6ae843e65c8ea24d Mon Sep 17 00:00:00 2001 From: nishaarya Date: Thu, 16 Apr 2020 13:33:58 +0100 Subject: [PATCH 6/6] part2 --- src/recursive_sorting/recursive_sorting.py | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index 3c09cd7a..d4a97f74 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -4,19 +4,12 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO + # Initialise pointers for the front of Arrays A & B a = 0 b = 0 - - return merged_arr - -def merge(arrA, arrB): - elements = len(arrA) + len(arrB) - merged_arr = [0] * elements - a = 0 - b = 0 - # since arrA and arrB already sorted, we only need to compare the first element of each when merging! for i in range(0, elements): - if a >= len(arrA): # all elements in arrA have been merged + # compare thearrays + if a >= len(arrA): # all elements in arrA have been merged merged_arr[i] = arrB[b] b += 1 elif b >= len(arrB): # all elements in arrB have been merged @@ -31,6 +24,16 @@ def merge(arrA, arrB): return merged_arr +def merge(arrA, arrB): + elements = len(arrA) + len(arrB) + merged_arr = [0] * elements + a = 0 + b = 0 + # since arrA and arrB already sorted, we only need to compare the first element of each when merging! + for i in range(0, elements): + + + # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO