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/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6a..5ef69bc7 100644 Binary files a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc and b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc differ diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..46f08678 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,62 @@ # TO-DO: Complete the selection_sort() function below -def selection_sort( arr ): + +def selection_sort(l): + # loop through n-1 elements - for i in range(0, len(arr) - 1): - cur_index = i - smallest_index = cur_index + 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 - # TO-DO: swap + return l +# TO-DO: implement the Bubble Sort function below +def bubble_sort(l): + swap_num = 1 + while swap_num > 0: + # reset swap num + swap_num = 0 - return arr + for i in range(len(l)-1): + # find smaller neighbors + if l[i] > l[i+1]: -# TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): + # swap + smaller_num = l[i+1] + l[i+1] = l[i] + l[i] = smaller_num - return arr + # update count + swap_num +=1 + + + return l # 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)) +print('bubble_sort') +print(bubble_sort(l))