Skip to content
Open
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
41 changes: 34 additions & 7 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,52 @@ def selection_sort( arr ):
smallest_index = cur_index
# TO-DO: find next smallest element
# (hint, can do in 3 loc)



for x in range(i,len(arr)):
if(arr[x] < arr[smallest_index]):
smallest_index = x
# TO-DO: swap



t = arr[cur_index]
arr[cur_index] = arr[smallest_index]
arr[smallest_index] = t

return arr


# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):
for i in range(0, len( arr )-1):
for x in range(0, len( arr )-1):
if(arr[x] > arr[x+1]):
t = arr[x]
arr[x] = arr[x+1]
arr[x+1] = t

return arr


# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum=-1 ):
if(len(arr) == 0):
return arr
elif(any([True if x < 0 else False for x in arr])):
return "Error, negative numbers not allowed in Count Sort"

# Make counting list of length = largest integer in arr
count_arr = [0]*(max(arr)+1)
out_arr = [0]*(len(arr))

# Count instances of each numberin the counting list
for x in arr:
count_arr[x] = count_arr[x] + 1

for x in range(0,len(count_arr)-1):
count_arr[x+1] = count_arr[x+1] + count_arr[x]

for x in arr[::-1]:
index = count_arr[x] - 1
out_arr[index] = x

arr = out_arr

return arr
20 changes: 10 additions & 10 deletions src/iterative_sorting/test_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ def test_bubble_sort(self):
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(arr2), [])
# self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort")
# self.assertEqual(count_sort(arr4), sorted(arr4))
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(arr2), [])
self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort")
self.assertEqual(count_sort(arr4), sorted(arr4))


if __name__ == '__main__':
Expand Down
49 changes: 47 additions & 2 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,73 @@
# TO-DO: complete the helpe function below to merge 2 sorted arrays
# TO-DO: complete the helper function below to merge 2 sorted arrays
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO
cA, cB, cM = 0,0,0

while(cA < len(arrA) and cB < len(arrB)):
if(arrA[cA] < arrB[cB]):
merged_arr[cM] = arrA[cA]
cA += 1
else:
merged_arr[cM] = arrB[cB]
cB += 1
cM += 1

if(cA < len(arrA)):
while(cM < len(merged_arr)):
merged_arr[cM] = arrA[cA]
cM +=1
cA +=1

if(cB < len(arrB)):
while(cM < len(merged_arr)):
merged_arr[cM] = arrB[cB]
cM +=1
cB +=1

return merged_arr


# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO
if(len(arr) <= 1):
return arr
else:
half = len(arr) // 2
arr1 = arr[half:]
arr2 = arr[:half]

sorted_1 = merge_sort(arr1)
sorted_2 = merge_sort(arr2)

return merge(sorted_1, sorted_2)

return arr


# STRETCH: implement an in-place merge sort algorithm
def merge_in_place(arr, start, mid, end):
# TO-DO

for i in range(start, end):
for x in range(start, end):
if(arr[x] > arr[x+1]):
t = arr[x]
arr[x] = arr[x+1]
arr[x+1] = t
return arr

def merge_sort_in_place(arr, l, r):
# TO-DO
m = (l+r)//2
if(r <= l):
return arr

merge_sort_in_place(arr, l, m)
merge_sort_in_place(arr, m+1, r)

merge_in_place(arr, l, m, r)
return arr


Expand Down
22 changes: 11 additions & 11 deletions src/recursive_sorting/test_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def test_merge_sort(self):
self.assertEqual(merge_sort(arr5), sorted(arr5))

# Uncomment this test to test your in-place merge sort implementation
# def test_in_place_merge_sort(self):
# arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
# arr2 = []
# arr3 = [2]
# arr4 = [0, 1, 2, 3, 4, 5]
# arr5 = random.sample(range(200), 50)
def test_in_place_merge_sort(self):
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
arr2 = []
arr3 = [2]
arr4 = [0, 1, 2, 3, 4, 5]
arr5 = random.sample(range(200), 50)

# self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9])
# self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), [])
# self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2])
# self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5])
# self.assertEqual(merge_sort_in_place(arr5, 0, len(arr5)-1), sorted(arr5))
self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9])
self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), [])
self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2])
self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5])
self.assertEqual(merge_sort_in_place(arr5, 0, len(arr5)-1), sorted(arr5))


if __name__ == '__main__':
Expand Down