diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..ff826a63 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/Users/anthonyhill/.local/share/virtualenvs/Sorting-YYiHKuic/bin/python" +} \ No newline at end of file diff --git a/Pipfile b/Pipfile new file mode 100644 index 00000000..1d79e13f --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +autopep8 = "*" +pylint = "*" + +[packages] + +[requires] +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 00000000..2cec7334 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,103 @@ +{ + "_meta": { + "hash": { + "sha256": "6c809087f55c30413ef2efc190ac156e327254cef0418ad3b1e3835b11e7b7a7" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": { + "astroid": { + "hashes": [ + "sha256:71ea07f44df9568a75d0f354c49143a4575d90645e9fead6dfb52c26a85ed13a", + "sha256:840947ebfa8b58f318d42301cf8c0a20fd794a33b61cc4638e28e9e61ba32f42" + ], + "version": "==2.3.3" + }, + "autopep8": { + "hashes": [ + "sha256:0f592a0447acea0c2b0a9602be1e4e3d86db52badd2e3c84f0193bfd89fd3a43" + ], + "index": "pypi", + "version": "==1.5" + }, + "isort": { + "hashes": [ + "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1", + "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd" + ], + "version": "==4.3.21" + }, + "lazy-object-proxy": { + "hashes": [ + "sha256:0c4b206227a8097f05c4dbdd323c50edf81f15db3b8dc064d08c62d37e1a504d", + "sha256:194d092e6f246b906e8f70884e620e459fc54db3259e60cf69a4d66c3fda3449", + "sha256:1be7e4c9f96948003609aa6c974ae59830a6baecc5376c25c92d7d697e684c08", + "sha256:4677f594e474c91da97f489fea5b7daa17b5517190899cf213697e48d3902f5a", + "sha256:48dab84ebd4831077b150572aec802f303117c8cc5c871e182447281ebf3ac50", + "sha256:5541cada25cd173702dbd99f8e22434105456314462326f06dba3e180f203dfd", + "sha256:59f79fef100b09564bc2df42ea2d8d21a64fdcda64979c0fa3db7bdaabaf6239", + "sha256:8d859b89baf8ef7f8bc6b00aa20316483d67f0b1cbf422f5b4dc56701c8f2ffb", + "sha256:9254f4358b9b541e3441b007a0ea0764b9d056afdeafc1a5569eee1cc6c1b9ea", + "sha256:9651375199045a358eb6741df3e02a651e0330be090b3bc79f6d0de31a80ec3e", + "sha256:97bb5884f6f1cdce0099f86b907aa41c970c3c672ac8b9c8352789e103cf3156", + "sha256:9b15f3f4c0f35727d3a0fba4b770b3c4ebbb1fa907dbcc046a1d2799f3edd142", + "sha256:a2238e9d1bb71a56cd710611a1614d1194dc10a175c1e08d75e1a7bcc250d442", + "sha256:a6ae12d08c0bf9909ce12385803a543bfe99b95fe01e752536a60af2b7797c62", + "sha256:ca0a928a3ddbc5725be2dd1cf895ec0a254798915fb3a36af0964a0a4149e3db", + "sha256:cb2c7c57005a6804ab66f106ceb8482da55f5314b7fcb06551db1edae4ad1531", + "sha256:d74bb8693bf9cf75ac3b47a54d716bbb1a92648d5f781fc799347cfc95952383", + "sha256:d945239a5639b3ff35b70a88c5f2f491913eb94871780ebfabb2568bd58afc5a", + "sha256:eba7011090323c1dadf18b3b689845fd96a61ba0a1dfbd7f24b921398affc357", + "sha256:efa1909120ce98bbb3777e8b6f92237f5d5c8ea6758efea36a473e1d38f7d3e4", + "sha256:f3900e8a5de27447acbf900b4750b0ddfd7ec1ea7fbaf11dfa911141bc522af0" + ], + "version": "==1.4.3" + }, + "mccabe": { + "hashes": [ + "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", + "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" + ], + "version": "==0.6.1" + }, + "pycodestyle": { + "hashes": [ + "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56", + "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c" + ], + "version": "==2.5.0" + }, + "pylint": { + "hashes": [ + "sha256:3db5468ad013380e987410a8d6956226963aed94ecb5f9d3a28acca6d9ac36cd", + "sha256:886e6afc935ea2590b462664b161ca9a5e40168ea99e5300935f6591ad467df4" + ], + "index": "pypi", + "version": "==2.4.4" + }, + "six": { + "hashes": [ + "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a", + "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c" + ], + "version": "==1.14.0" + }, + "wrapt": { + "hashes": [ + "sha256:565a021fd19419476b9362b05eeaa094178de64f8361e44468f9e9d7843901e1" + ], + "version": "==1.11.2" + } + } +} diff --git a/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc b/src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc index 7176fe6a..5116c5f2 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..ac774fb3 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,47 @@ -# TO-DO: Complete the selection_sort() function below -def selection_sort( 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): cur_index = i smallest_index = cur_index # TO-DO: find next smallest element - # (hint, can do in 3 loc) - - - + # (hint, can do in 3 loc) + # progress through array looking for smaller number + for j in range(cur_index, len(arr)): + if arr[smallest_index] > arr[j]: + smallest_index = j # TO-DO: swap - - - + arr[cur_index], arr[smallest_index] = arr[smallest_index], arr[cur_index] return arr # TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): +# UNDERSTAND + # Compare consecutive items + # The highest number will bubble all the way to the right with each iteration + + +def bubble_sort(arr): + # PLAN + # create first iteration that takes in the first index + for i in range(1, len(arr)): + # make another iteration that is always at the prev number + for j in range(0, len(arr) - 1): + # if 2nd number is greater than the first, swap + if arr[j] > arr[j + 1]: + arr[j], arr[j + 1] = arr[j + 1], arr[j] + # i += 1 return arr +a = [8, 2, 5, 1] +bubble_sort(a) + + # STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): +def count_sort(arr, maximum=-1): - return arr \ No newline at end of file + return arr diff --git a/src/iterative_sorting/test_iterative.py b/src/iterative_sorting/test_iterative.py index df11b648..437c41b7 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -2,6 +2,7 @@ import random from iterative_sorting import * + class IterativeSortingTest(unittest.TestCase): def test_selection_sort(self): arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] @@ -9,9 +10,9 @@ def test_selection_sort(self): arr3 = [0, 1, 2, 3, 4, 5] arr4 = random.sample(range(200), 50) - self.assertEqual(selection_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) + self.assertEqual(selection_sort(arr1), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(selection_sort(arr2), []) - self.assertEqual(selection_sort(arr3), [0,1,2,3,4,5]) + self.assertEqual(selection_sort(arr3), [0, 1, 2, 3, 4, 5]) self.assertEqual(selection_sort(arr4), sorted(arr4)) def test_bubble_sort(self): @@ -20,21 +21,23 @@ def test_bubble_sort(self): 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(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(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): # arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] # arr2 = [] # arr3 = [1, 5, -2, 4, 3] # arr4 = random.sample(range(200), 50) - # self.assertEqual(count_sort(arr1), [0,1,2,3,4,5,6,7,8,9]) + # self.assertEqual(count_sort(arr1), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # self.assertEqual(count_sort(arr2), []) - # self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort") + # self.assertEqual(count_sort( + # arr3), "Error, negative numbers not allowed in Count Sort") # self.assertEqual(count_sort(arr4), sorted(arr4)) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..3891eb5d 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -1,26 +1,67 @@ -# TO-DO: complete the helpe function below to merge 2 sorted arrays -def merge( arrA, arrB ): - elements = len( arrA ) + len( arrB ) - merged_arr = [0] * elements +# TO-DO: complete the helper function below to merge 2 sorted arrays +import math + + +def merge(arrA, arrB): + elements = len(arrA) + len(arrB) + merged_arr = [] # TO-DO - + # create pointers for each side, starting at 0. + # Left and right will only increment once an item is added to merged_arr + left = 0 + right = 0 + while left < len(arrA) and right < len(arrB): + if arrA[left] <= arrB[right]: + merged_arr.append(arrA[left]) + left += 1 + else: + merged_arr.append(arrB[right]) + right += 1 + # NEED TO MAKE SURE TO CONTINUE TO EMPTY ARRAYS + # BOTH LEFT AND RIGHT ARRAYS MUST BE EMPTIED FOR IT TO BE FULLY MERGED IN MERGED_ARR + while left < len(arrA): + merged_arr.append(arrA[left]) + left += 1 + while right < len(arrB): + merged_arr.append(arrB[right]) + right += 1 return merged_arr +a = [1, 3, 4, 5] +b = [2, 9, 7, 8] +c = [2, 9, 7, 8, 3, 5] +# print(merge(a, b)) + # TO-DO: implement the Merge Sort function below USING RECURSION -def merge_sort( arr ): - # TO-DO - return arr +# def merge_sort(arr): +# # TO-DO + +# return arr + +def merge_sort(arr): + if len(arr) < 2: + return arr + mid = len(arr) // 2 + left = arr[0:mid] + right = arr[mid:len(arr)] + return merge(merge_sort(left), merge_sort(right)) + + +print(merge_sort(c)) # STRETCH: implement an in-place merge sort algorithm + + def merge_in_place(arr, start, mid, end): # TO-DO return arr -def merge_sort_in_place(arr, l, r): + +def merge_sort_in_place(arr, l, r): # TO-DO return arr @@ -28,6 +69,6 @@ def merge_sort_in_place(arr, l, r): # STRETCH: implement the Timsort function below # hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt -def timsort( arr ): +def timsort(arr): return arr