Skip to content
Open

Hill #265

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/Users/anthonyhill/.local/share/virtualenvs/Sorting-YYiHKuic/bin/python"
}
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -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"
103 changes: 103 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc
Binary file not shown.
41 changes: 29 additions & 12 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -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
return arr
15 changes: 9 additions & 6 deletions src/iterative_sorting/test_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
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]
arr2 = []
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):
Expand All @@ -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))


Expand Down
61 changes: 51 additions & 10 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,74 @@
# 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


# 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