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
51 changes: 35 additions & 16 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
# 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




return arr
# # loop through n-1 elements
# # len(arr) => 5
# # out of bounds
# # len(arr) - 1
# # 4 -> 3
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)
# # iterate over a range
for j in range(cur_index, len(arr)):
if arr[j] < arr[smallest_index]:
smallest_index = j


# # swap
arr[smallest_index], arr[cur_index] = arr[cur_index], arr[smallest_index]

return arr


# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):
n = len(arr)

for i in range(n):
for j in range(n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j] ## keep swapping the order and change the position ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ) an so on

return arr


# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum=-1 ):
m = maximum = 0
count = [0] * m
for a in arr:
count[a] += 1
i = 0
for a in range(m):
for c in range(count[a]):
arr[i] = a
i += 1

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

L = 0
R = 0
for i in range(0, elements):
if L >= len(arrA):
merged_arr[i] = arrB[R]
R += 1
elif R >= len(arrB):
merged_arr[i] = arrA[L]
L += 1
elif arrA[L] < arrB[R]:
merged_arr[i] = arrA[L]
L += 1
else:
merged_arr[i] = arrB[R]
R += 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]

ms_1 = merge_sort(arr1)
ms_2 = merge_sort(arr2)

return merge(ms_1, ms_2)
return arr


Expand Down
4 changes: 4 additions & 0 deletions src/searching/searching.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ def linear_search(arr, target):

# TO-DO: add missing code

for value in arr:
if value == target:
return 1

return -1 # not found


Expand Down
2 changes: 1 addition & 1 deletion src/searching/test_searching.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import unittest
import unittest
from searching import *


Expand Down