From e3239dc3dbf3bdb7e6466b06aa02b431e2857fdc Mon Sep 17 00:00:00 2001 From: SarmenSinanian Date: Wed, 22 Jan 2020 17:19:48 -0800 Subject: [PATCH 1/2] CS25 Sprint 2 Sortin Homework Submission --- src/insertion_sort.py | 27 +++ src/iterative_binary_search.py | 41 ++++ .../iterative_sorting.cpython-37.pyc | Bin 815 -> 1168 bytes src/iterative_sorting/asdf.py | 86 +++++++++ src/iterative_sorting/iterative_sorting.py | 179 ++++++++++++++++-- src/recursive_binary_search.py | 38 ++++ 6 files changed, 357 insertions(+), 14 deletions(-) create mode 100644 src/insertion_sort.py create mode 100644 src/iterative_binary_search.py create mode 100644 src/iterative_sorting/asdf.py create mode 100644 src/recursive_binary_search.py diff --git a/src/insertion_sort.py b/src/insertion_sort.py new file mode 100644 index 00000000..d5853ea1 --- /dev/null +++ b/src/insertion_sort.py @@ -0,0 +1,27 @@ +def insertion_sort(items): + # Split the list into sorted and unsorted + # For each element in unsorted... + counter = 0 + for i in range(1, len(items)): + # Insert that element into the correct place in sorted + # Store the elements in a temp variable + temp = items[i] + # Shifting all larger sorted elements to the right by 1 + j = i + while j > 0 and temp < items[j - 1]: + print('**********************') + counter += 1 + print(items) + items[j] = items[j - 1] + j -= 1 + print(items) + # Insert the element after the first smaller element + items[j] = temp + print(items) + print(f'Counter = {counter}') + return items + + +l = [7, 4, 9, 2, 6, 3, 0, 8, 5, 1] + +insertion_sort(l) \ No newline at end of file diff --git a/src/iterative_binary_search.py b/src/iterative_binary_search.py new file mode 100644 index 00000000..1ed422cd --- /dev/null +++ b/src/iterative_binary_search.py @@ -0,0 +1,41 @@ +# Assume names is sorted +# Assume names is a list +def binary_search(to_find, names): + # Cut our list in half, examine the midpoint item + start = 0 + end = len(names) + while end - start > 0: + mid = start + (end - start) // 2 + item = names[mid] + print(start) + print(mid) + print(end) + print('***') + # If the item is equal to to_find: + if item == to_find: + return True + # Otherwise, if it's smaller + elif item > to_find: + end = mid - 1 + # Repeat binary search on first half of the list + # Otherwise + else: + start = mid + 1 + # Repeat binary search on second half + return False + + +names = ['Jack', 'Jill', 'Joe', 'James', 'Jessica', 'Jones', 'Jeremy', 'Jamie'] + + +def sorting(list): + list.sort() + return list + + +names = sorting(names) + +print(names) +to_find = ['Jamie'] + +print(binary_search('Jamie', names)) \ No newline at end of file diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6aa9951159b6a50bac8891d62dd4090bdc..ccc7c33efad0b2b4793f7203a75adf8e81ea3b99 100644 GIT binary patch literal 1168 zcmaJ>&2G~`5Z?7q;-pOwP|}tH+8cyJ1stJ8l(ZF~!s-Hrhx? zngO zSjjyAPB`Tn*SWz>UgBkL@k+Pq5MJd|ABjVSCa56C^q5k@r)|=#ot7c&)Ig{pXb2?) z9RU_OwGd1M3g9E)K!1#G!AhpV`iUMIDO8YB1v$7jM~<~s^6KY^rce{+RCJ^%ZKMM? zly*WsYiI>#Dt*EZzop>EwOKOO$u+)=4Dc%D8bs;yWMnF$40x8nURK5=8knWbGpwRY z6Q)d6dS^%ne5!>KDwIJ96)mt&sqzVQMp+PJXcBTf$mvicA!N)&J(+cXr8s zW0xoboxRkS*c^%PXL$4Oe@1n9b3Kqr%JyQ(!Z;o<|3FF+r7VcJIAp_H{~RZMFAN0) z6zB7~xV_feYOk{Otru)_Yim2V4Vf+cIN}$jdz0W<`@EV6MIUnyZ(j(F`vXr3UPS(J zY~-mn)|tJtva|Mlt-UkOeQjfpC8?K=_3|`qnGL5Nu}x2Qg+y}RXGKcy2+a#U=A}$P z;qe{|&dMyarkKn79TtYo?;Wkv(10^RH6opI`uPK|j+drsrLsZ-p- zsrkk2Isp{yn7Nx%3iC?85_uV3R`q zu?+crchl>?;a+DYNrG;KElu2(&Fs8f_Q17cnFdkUO{DKaO6jG+d(lY>>jlf=;3%sn zP_3T^ag_Tt@Vi?CAlh|mQ3GZj<{a#C;=W$&Ejm-!8oIm+5$&cf>+sBptlYsMjK|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~ right) and ((j + 1) < (len(arr) - 1)): + # print('if left > right: swap') + # print(f'if statement #{j}') + arr[j] = right + arr[j+1] = left + j += 1 + left = arr[j] + # print(left) + # print(arr[j]) + # print(arr[j+1]) + right = arr[j+1] + # print(arr) + # print('^Swapped array print ^') + # print(f'Post-Swap: temp-Left = ({left}); temp-Right = ({right})') + # print(f'Post-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') + if (left > right) and ((j + 1) == (len(arr) - 1)): + arr[j] = right + arr[j + 1] = left + left = arr[j] + right = arr[j + 1] + # print(arr) + # print(left) + # print(right) + # print(f'j = {j}') + # print('WOW') + j += 1 + else: + # print('break') + break + if (right > left) and ((j + 1) < (len(arr) - 1)): + # print('if right > left: move on to next') + # print(f'if statement #{j}') + j += 1 + left = arr[j] + right = arr[j+1] + # print(arr) + # print('^Same array print ^') + # print(f'No-Swap: temp-Left = ({left}); temp-Right = ({right})') + # print(f'No-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') + if right == left: + # print('Right == Left') + pass + else: + # print('Else statement') + break + break + # print(f'i @ end = {i}') + # print(f'j @ end = {j}') + return arr + +print(bubble_sort(arr)) + +import random +l = list(range(10000)) +random.shuffle(l) +l_copy = l.copy() + +import time +start_time = time.time() +print(bubble_sort(l_copy)) +end_time = time.time() +print(f'runtime: {end_time - start_time}') diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..5d18c851 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,181 @@ -# TO-DO: Complete the selection_sort() function below -def selection_sort( arr ): +# Selection sort: first item in array checks second item to see if first item is larger or not. If first item is larger, +# first item and second item switch positions in array. Then the recently switched item checks itself in the same manner +# against all the other numbers until switched and then that switched number does the same. + + + +# # # 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 +# # return arr +# + + +arr = [7, 4, 9, 2, 6, 3, 0, 8, 5, 1] + +def selection_sort(arr): # loop through n-1 elements for i in range(0, len(arr) - 1): + print('****************************\n') cur_index = i smallest_index = cur_index - # TO-DO: find next smallest element - # (hint, can do in 3 loc) - + print(f'First for loop current index {cur_index}') + print(f'First for loop smallest index {smallest_index}') + for j in range((i+1), len(arr)): + print('*****SECOND FOR LOOP*****') + smallest_index_element = arr[smallest_index] + compared_item = arr[j] + print(f'Second for loop current j index {j}') + print(f'Smallest index element {smallest_index_element}') + print(f'Second for loop compared index element {compared_item}') + if smallest_index_element > compared_item: + print('*****IF STATEMENT*****') + small = arr[j] + larger = arr[smallest_index] + print(f'If statement Small = {small}; Larger = {larger}') + arr[smallest_index] = small + arr[j] = larger + print(f'If statement Array = {arr}') + print(f'j at end of if statement = {j}') + return arr - # TO-DO: swap +print(selection_sort(arr)) - return arr +# arr[j] = right +# arr[j+1] = left +# j += 1 +# left = arr[j] +# # print(left) +# # print(arr[j]) +# # print(arr[j+1]) +# right = arr[j+1] -# TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): - - return 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): +# j = i +# cur_index = i +# currently_compared = j +# smallest_index = cur_index +# next_index = (i + 1) +# left = arr[smallest_index] +# print(f'Smallest index: = {smallest_index}') +# print(f'Next index: = {next_index}') +# right = arr[next_index] +# if (left > right) and ((j) < (len(arr) - 1)): +# arr[i] = right +# arr[i+1] = left +# left = arr[i] +# print(left) +# print(arr[i]) +# print(arr[i + 1]) +# right = arr[i + 1] +# j += 1 +# if right > left: +# pass +# if left == right: +# pass +# else: +# pass +# # TO-DO: find next smallest element +# # (hint, can do in 3 loc) +# # TO-DO: swap +# return arr +# arr = [7, 4, 9, 2, 6, 3, 0, 8, 5, 1] +# print(selection_sort(arr)) -# STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): +# # TO-DO: implement the Bubble Sort function below +# +# def bubble_sort(arr): +# # Create temporary items 1 and 2 (one per compared item) +# # If item 2 is smaller than item 1, item 2 is swapped to the left of item 1 +# for i in range(len(arr)): +# # print(arr[9]) +# # j = i +# # print('********************') +# # print(f'i @ beginning = {i}') +# # print(f'j @ beginning = {j}') +# for z in range(len(arr)): +# j = z +# # print(f'J ({j}) = Z ({z})') +# # print(f'Z = ({z})') +# while j < (len(arr) - 1): +# left = arr[j] +# # print(arr) +# # print(j) +# right = arr[j + 1] +# # print(f'while loop #{j}') +# # print(f'Pre-Swap: temp-Left = ({left}); temp-Right = ({right})') +# # print(f'Pre-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') +# for m in range(len(arr)): +# if (left > right) and ((j + 1) < (len(arr) - 1)): +# # print('if left > right: swap') +# # print(f'if statement #{j}') +# arr[j] = right +# arr[j+1] = left +# j += 1 +# left = arr[j] +# # print(left) +# # print(arr[j]) +# # print(arr[j+1]) +# right = arr[j+1] +# # print(arr) +# # print('^Swapped array print ^') +# # print(f'Post-Swap: temp-Left = ({left}); temp-Right = ({right})') +# # print(f'Post-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') +# if (left > right) and ((j + 1) == (len(arr) - 1)): +# arr[j] = right +# arr[j + 1] = left +# left = arr[j] +# right = arr[j + 1] +# # print(arr) +# # print(left) +# # print(right) +# # print(f'j = {j}') +# # print('WOW') +# j += 1 +# else: +# # print('break') +# break +# if (right > left) and ((j + 1) < (len(arr) - 1)): +# # print('if right > left: move on to next') +# # print(f'if statement #{j}') +# j += 1 +# left = arr[j] +# right = arr[j+1] +# # print(arr) +# # print('^Same array print ^') +# # print(f'No-Swap: temp-Left = ({left}); temp-Right = ({right})') +# # print(f'No-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') +# if right == left: +# # print('Right == Left') +# pass +# else: +# # print('Else statement') +# break +# break +# # print(f'i @ end = {i}') +# # print(f'j @ end = {j}') +# return arr +# +# print(bubble_sort(arr)) +# +# # STRETCH: implement the Count Sort function below +# def count_sort( arr, maximum=-1 ): +# +# return arr - return arr \ No newline at end of file diff --git a/src/recursive_binary_search.py b/src/recursive_binary_search.py new file mode 100644 index 00000000..83011b27 --- /dev/null +++ b/src/recursive_binary_search.py @@ -0,0 +1,38 @@ +# Assume names is sorted +# Assume names is a list +def binary_search(to_find, names): + # If the list is empty + if len(names) == 0: + return False + # Cut our list in half, examine the midpoint item + mid = len(names) // 2 + item = names[mid] + # If the item is equal to to_find: + print(item) + print(names) + if item == to_find: + return True + # Otherwise, if it's smaller + elif item > to_find: + return binary_search(to_find, names[:mid]) + # Repeat binary search on first half of the list + # Otherwise + else: + return binary_search(to_find, names[mid+1:]) + # Repeat binary search on second half + + +names = ['Jack', 'Jill', 'Joe', 'James', 'Jessica', 'Jones', 'Jeremy', 'Jamie'] + + +def sorting(list): + list.sort() + return list + + +names = sorting(names) + +print(names) +to_find = ['Jamie'] + +print(binary_search('Jamie', names)) \ No newline at end of file From b9719ec277aad323ab38df3d653e6098fb3befb6 Mon Sep 17 00:00:00 2001 From: SarmenSinanian Date: Mon, 13 Apr 2020 15:45:35 -0700 Subject: [PATCH 2/2] Updated iterative_sorting --- src/iterative_sorting/iterative_sorting.py | 122 ++++++++++----------- 1 file changed, 56 insertions(+), 66 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 5d18c851..81d55aeb 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -49,57 +49,46 @@ def selection_sort(arr): print(selection_sort(arr)) +# NO PRINT STATEMENT VERSION +def bubble_sort(arr): + # Create temporary items 1 and 2 (one per compared item) + # If item 2 is smaller than item 1, item 2 is swapped to the left of item 1 + for i in range(len(arr)): + for z in range(len(arr)): + while z < (len(arr) - 1): + left = arr[z] + right = arr[z + 1] + for m in range(len(arr)): + if (left > right) and ((z + 1) < (len(arr) - 1)): + arr[z] = right + arr[z+1] = left + z += 1 + left = arr[z] + right = arr[z+1] + if (left > right) and ((z + 1) == (len(arr) - 1)): + arr[z] = right + arr[z + 1] = left + left = arr[z] + right = arr[z + 1] + z += 1 + else: + break + if (right > left) and ((z + 1) < (len(arr) - 1)): + z += 1 + left = arr[z] + right = arr[z+1] + if right == left: + pass + else: + break + break + return arr +print(bubble_sort(arr)) -# arr[j] = right -# arr[j+1] = left -# j += 1 -# left = arr[j] -# # print(left) -# # print(arr[j]) -# # print(arr[j+1]) -# right = arr[j+1] - - -# # 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): -# j = i -# cur_index = i -# currently_compared = j -# smallest_index = cur_index -# next_index = (i + 1) -# left = arr[smallest_index] -# print(f'Smallest index: = {smallest_index}') -# print(f'Next index: = {next_index}') -# right = arr[next_index] -# if (left > right) and ((j) < (len(arr) - 1)): -# arr[i] = right -# arr[i+1] = left -# left = arr[i] -# print(left) -# print(arr[i]) -# print(arr[i + 1]) -# right = arr[i + 1] -# j += 1 -# if right > left: -# pass -# if left == right: -# pass -# else: -# pass -# # TO-DO: find next smallest element -# # (hint, can do in 3 loc) -# # TO-DO: swap -# return arr -# arr = [7, 4, 9, 2, 6, 3, 0, 8, 5, 1] -# print(selection_sort(arr)) -# # TO-DO: implement the Bubble Sort function below -# # def bubble_sort(arr): # # Create temporary items 1 and 2 (one per compared item) # # If item 2 is smaller than item 1, item 2 is swapped to the left of item 1 @@ -110,53 +99,53 @@ def selection_sort(arr): # # print(f'i @ beginning = {i}') # # print(f'j @ beginning = {j}') # for z in range(len(arr)): -# j = z +# # j = z # # print(f'J ({j}) = Z ({z})') # # print(f'Z = ({z})') -# while j < (len(arr) - 1): -# left = arr[j] +# while z < (len(arr) - 1): +# left = arr[z] # # print(arr) # # print(j) -# right = arr[j + 1] +# right = arr[z + 1] # # print(f'while loop #{j}') # # print(f'Pre-Swap: temp-Left = ({left}); temp-Right = ({right})') # # print(f'Pre-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') # for m in range(len(arr)): -# if (left > right) and ((j + 1) < (len(arr) - 1)): +# if (left > right) and ((z + 1) < (len(arr) - 1)): # # print('if left > right: swap') # # print(f'if statement #{j}') -# arr[j] = right -# arr[j+1] = left -# j += 1 -# left = arr[j] +# arr[z] = right +# arr[z+1] = left +# z += 1 +# left = arr[z] # # print(left) # # print(arr[j]) # # print(arr[j+1]) -# right = arr[j+1] +# right = arr[z+1] # # print(arr) # # print('^Swapped array print ^') # # print(f'Post-Swap: temp-Left = ({left}); temp-Right = ({right})') # # print(f'Post-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})') -# if (left > right) and ((j + 1) == (len(arr) - 1)): -# arr[j] = right -# arr[j + 1] = left -# left = arr[j] -# right = arr[j + 1] +# if (left > right) and ((z + 1) == (len(arr) - 1)): +# arr[z] = right +# arr[z + 1] = left +# left = arr[z] +# right = arr[z + 1] # # print(arr) # # print(left) # # print(right) # # print(f'j = {j}') # # print('WOW') -# j += 1 +# z += 1 # else: # # print('break') # break -# if (right > left) and ((j + 1) < (len(arr) - 1)): +# if (right > left) and ((z + 1) < (len(arr) - 1)): # # print('if right > left: move on to next') # # print(f'if statement #{j}') -# j += 1 -# left = arr[j] -# right = arr[j+1] +# z += 1 +# left = arr[z] +# right = arr[z+1] # # print(arr) # # print('^Same array print ^') # # print(f'No-Swap: temp-Left = ({left}); temp-Right = ({right})') @@ -173,6 +162,7 @@ def selection_sort(arr): # return arr # # print(bubble_sort(arr)) + # # # STRETCH: implement the Count Sort function below # def count_sort( arr, maximum=-1 ):