From 95aff1b9472cca485d920e7dfee79e25f785978f Mon Sep 17 00:00:00 2001 From: Elizabeth Ter Sahakyan Date: Tue, 17 Mar 2020 23:34:15 -0700 Subject: [PATCH 1/3] linear, binary search and insertion sort --- .vscode/settings.json | 3 + Pipfile | 11 ++++ src/bigo/cs27_notes.py | 77 ++++++++++++++++++++++ src/iterative_sorting/iterative_sorting.py | 12 +++- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 Pipfile create mode 100644 src/bigo/cs27_notes.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..022d2870 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/Applications/anaconda3/envs/conda-pipenv/bin/python" +} \ No newline at end of file diff --git a/Pipfile b/Pipfile new file mode 100644 index 00000000..b9ba84f6 --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/src/bigo/cs27_notes.py b/src/bigo/cs27_notes.py new file mode 100644 index 00000000..2ad3ff7b --- /dev/null +++ b/src/bigo/cs27_notes.py @@ -0,0 +1,77 @@ +# search + +def linear_search(l, value): + + for i in range(len(l)): + if i == value: + return True + + return False + +def binary_search(l, value): + if len(l) <= 1: + pass + + # define first and last indices + first = 0 + last = len(l) - 1 + + # variable to store if found + found = False + + # when the first item of the list is not the last item + # and found is not True + while first <= last and not found: + print(1) + middle = ( first + last ) // 2 + + if value == l[middle]: + found = True + + else: + if value < l[middle]: + last = middle - 1 + else: + first = middle + 1 + + return found + + +# insertion sort + +def insertion_sort(l): + # separate first element, think of it as sorted + + + # for all other indices, starting at 1 + for i in range(1, len(l)): + + # put current number in a temp var + temp = l[i] + + # look left, until we find position + j = i + while j > 0 and temp < l[j-1]: + l[j] = l[j-1] + j -= 1 + + # as we look left, shift items right + + # when left is smaller than temp, or we're at zero, put at this spot + l[j] = temp + + return l + +l = [3,4,6,1,2,9,4,6,7,0,5] +l2 = sorted(l) +print(l) +value = 4 +print('linear') +print(linear_search(l2, value)) +print('binary') +print(binary_search(l2, value)) +print('insertion sort') +print(f'list: {l}') +print(insertion_sort(l)) + + diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..d105e377 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -6,7 +6,17 @@ def selection_sort( arr ): smallest_index = cur_index # TO-DO: find next smallest element # (hint, can do in 3 loc) - + + middle_index = len(arr) // 2 + + if arr[i] > arr[middle_index]: + + + # iterate through a loop to compare + + # check if the element is smaller + # + # if smaller, swap the elements From 62f82d57857f35087b3f92f7564dcc9dc70e53e5 Mon Sep 17 00:00:00 2001 From: Elizabeth Ter Sahakyan Date: Wed, 18 Mar 2020 10:51:45 -0700 Subject: [PATCH 2/3] selection sort test passed --- .../iterative_sorting.cpython-37.pyc | Bin 815 -> 792 bytes src/iterative_sorting/iterative_sorting.py | 45 +++++++++--------- src/iterative_sorting/test_iterative.py | 18 +++---- 3 files changed, 32 insertions(+), 31 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..02fbc1173d0b0f95041978892a2bb5e9274b56f9 100644 GIT binary patch literal 792 zcmZ`%OOMkq5T3D3?w#Mk zU&@tJ{sI@6@wR9MVx$>={yd(S{jA^b0LizhdH+TN{NT+&x&)q*+LvS$D7=9Llu(jZ zR1Z|BS~r*o)mD)@QXSP*J&*copzbZYiPV9@9U|yJgO)pw)%{C|AI_0Nvb0D;(hE|% zA+s@#GC8bZhe`}#C!WK*w_B`mD=MhO32bF0w!!AiLACeuKBtHCN`3-!e%JTv{58-X z>26p_Mq{W#T7xlM9rGF0Bqt4}hlVSQMID;7Sm;{jx`RMYjEy`um z+vBWIdXq%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[middle_index]: - - - # iterate through a loop to compare - - # check if the element is smaller - # - # if smaller, swap the elements - +def selection_sort(l): - # TO-DO: swap + # loop through n-1 elements + for i in range(0, len(l) - 1): + j = i + # TO-DO: find next smallest element + # (hint, can do in 3 loc) + lowest_num = l[j] + lowest_index = j + for j in range(i, len(l)): + if l[j] < lowest_num: + lowest_num = l[j] + lowest_index = j + # swap the lowest with the current position + swap_num = l[i] + l[i] = lowest_num + l[lowest_index] = swap_num - return arr + return l # TO-DO: implement the Bubble Sort function below @@ -37,4 +33,9 @@ def bubble_sort( arr ): # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): - return arr \ No newline at end of file + return arr + + +l = [3,4,6,1,2,9,4,6,7,0,5] +print('selection_sort') +print(selection_sort(l)) diff --git a/src/iterative_sorting/test_iterative.py b/src/iterative_sorting/test_iterative.py index df11b648..204f429a 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -14,16 +14,16 @@ def test_selection_sort(self): self.assertEqual(selection_sort(arr3), [0,1,2,3,4,5]) self.assertEqual(selection_sort(arr4), sorted(arr4)) - def test_bubble_sort(self): - arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] - arr2 = [] - arr3 = [0, 1, 2, 3, 4, 5] - arr4 = random.sample(range(200), 50) + # def test_bubble_sort(self): + # arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] + # arr2 = [] + # arr3 = [0, 1, 2, 3, 4, 5] + # arr4 = random.sample(range(200), 50) - self.assertEqual(bubble_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) - self.assertEqual(bubble_sort(arr2), []) - self.assertEqual(bubble_sort(arr3), [0,1,2,3,4,5]) - self.assertEqual(bubble_sort(arr4), sorted(arr4)) + # self.assertEqual(bubble_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) + # self.assertEqual(bubble_sort(arr2), []) + # self.assertEqual(bubble_sort(arr3), [0,1,2,3,4,5]) + # self.assertEqual(bubble_sort(arr4), sorted(arr4)) # Uncomment this test to test your count_sort implementation # def test_counting_sort(self): From 8557de23e530840b888de9f019188644f5c78737 Mon Sep 17 00:00:00 2001 From: Elizabeth Ter Sahakyan Date: Wed, 18 Mar 2020 11:02:07 -0700 Subject: [PATCH 3/3] bubble sort test passed --- .../iterative_sorting.cpython-37.pyc | Bin 792 -> 986 bytes src/iterative_sorting/iterative_sorting.py | 25 ++++++++++++++++-- src/iterative_sorting/test_iterative.py | 18 ++++++------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 02fbc1173d0b0f95041978892a2bb5e9274b56f9..5ef69bc71c10d41ad6b9be24d301abf285a3e741 100644 GIT binary patch delta 422 zcmY*Uy-or_5T4mxU^(EVK;@`O%oz&{i4Cz(5(^U>6Cosu7xoH~1KB$wF?X>bvA6tP^+O zu9L7BV&;Hhh5O(DbvHLr!nf;fh}RkNx3uOI5}Y-=Fw1vo#SOUpS~EJXjVzaGZr(HCfpX+pX6q1{7#D|=^`SjxUw<79K^|Cni&?O>|rWB<&E4Q(RnNXN=G IP-2!p4fJSFN&kMJh31?uQYchLlMX19L6d> zkji96ka8ekocxDL!iWRN(qz2FoLE#;1Y$!7P9W|lE4oq+o80`A Y(wtN~W{^D~XL3k#uyL_UF>){h0IYy8ApigX diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 2593367e..46f08678 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -25,9 +25,28 @@ def selection_sort(l): # TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): +def bubble_sort(l): + swap_num = 1 + while swap_num > 0: - return arr + # reset swap num + swap_num = 0 + + for i in range(len(l)-1): + + # find smaller neighbors + if l[i] > l[i+1]: + + # swap + smaller_num = l[i+1] + l[i+1] = l[i] + l[i] = smaller_num + + # update count + swap_num +=1 + + + return l # STRETCH: implement the Count Sort function below @@ -39,3 +58,5 @@ def count_sort( arr, maximum=-1 ): l = [3,4,6,1,2,9,4,6,7,0,5] print('selection_sort') print(selection_sort(l)) +print('bubble_sort') +print(bubble_sort(l)) diff --git a/src/iterative_sorting/test_iterative.py b/src/iterative_sorting/test_iterative.py index 204f429a..df11b648 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -14,16 +14,16 @@ def test_selection_sort(self): self.assertEqual(selection_sort(arr3), [0,1,2,3,4,5]) self.assertEqual(selection_sort(arr4), sorted(arr4)) - # def test_bubble_sort(self): - # arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] - # arr2 = [] - # arr3 = [0, 1, 2, 3, 4, 5] - # arr4 = random.sample(range(200), 50) + def test_bubble_sort(self): + arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] + arr2 = [] + arr3 = [0, 1, 2, 3, 4, 5] + arr4 = random.sample(range(200), 50) - # self.assertEqual(bubble_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) - # self.assertEqual(bubble_sort(arr2), []) - # self.assertEqual(bubble_sort(arr3), [0,1,2,3,4,5]) - # self.assertEqual(bubble_sort(arr4), sorted(arr4)) + self.assertEqual(bubble_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) + self.assertEqual(bubble_sort(arr2), []) + self.assertEqual(bubble_sort(arr3), [0,1,2,3,4,5]) + self.assertEqual(bubble_sort(arr4), sorted(arr4)) # Uncomment this test to test your count_sort implementation # def test_counting_sort(self):