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
44 changes: 25 additions & 19 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# 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)




# TO-DO: swap

for i in range(len(arr)-1):
sm_i = min(enumerate(arr[i:], start=i), key=lambda x: x[1])[0]
arr[i], arr[sm_i] = arr[sm_i], arr[i]

return arr

def bubble_sort( arr ):
elem_num = len(arr)
sorted_elems = 0
while sorted_elems != elem_num:
for i in range(elem_num - sorted_elems - 1):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted_elems += 1

return arr

def count_sort( arr, maximum=-1 ):
if arr == []:
return []

# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):
if min(arr) < 0:
return 'Error, negative numbers not allowed in Count Sort'

return arr
cntArr = [0] * (max(arr)+1)

for e in arr:
cntArr[e]+=1

# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum=-1 ):
index = 0
for i, e in enumerate(cntArr):
arr[index:index+e] = [i]*e
index+=e

return 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
39 changes: 36 additions & 3 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,49 @@
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO

iM, iA, iB = 0, 0, 0
while iA < len( arrA ) and iB < len( arrB ):
if arrA[iA] < arrB[iB]:
merged_arr[iM] = arrA[iA]
iA += 1
iM += 1
else:
merged_arr[iM] = arrB[iB]
iB += 1
iM += 1

if iA == len( arrA ):
merged_arr[iM:] = arrB[iB:]
else:
merged_arr[iM:] = arrA[iA:]

return merged_arr


# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO
if arr == []:
return []

return arr
if not isinstance(arr[0], list):
arr = list(map(lambda x: [x], arr))

if len(arr) == 1:
return arr[0]

if len(arr) % 2 == 0:
split1 = arr[:len(arr)//2]
split2 = arr[len(arr)//2:]
rem = []
else:
split1 = arr[:len(arr)//2]
split2 = arr[len(arr)//2:-1]
rem = [arr[-1]]

merged = [ merge(a, b) for a, b in zip(split1, split2) ] + rem

return merge_sort(merged)


# STRETCH: implement an in-place merge sort algorithm
Expand Down