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
20 changes: 16 additions & 4 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@
def selection_sort( arr ):
# loop through n-1 elements
for i in range(0, len(arr) - 1):
cur_index = i
cur_index = i
smallest_index = cur_index
# TO-DO: find next smallest element
# (hint, can do in 3 loc)


# i + 1 makes it so we start at 1 and not 0, b/c lists, aka arrays start at 0.
for j in range(i + 1, len(arr)): # i + 1 = 0 + 1, length(arr)
if arr[j] < arr[smallest_index]: # j ( is less than) smallest_index (1)
smallest_index = j # 1 = 1


# TO-DO: swap


arr[i], arr[smallest_index] = arr[smallest_index], arr[i] # 0, 0 = 0, 0


return arr


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

#length of list (array)
bubble = len(arr) - 1
sorted = False
while not sorted:
sorted = True
for i in range(0, bubble):
#item on left greater than item on right, set sorted to false, flip items.
if arr[i] > arr[i + 1]:
sorted = False
arr[i], arr[i + 1] = arr[i + 1], arr[i]
return arr


Expand Down
28 changes: 27 additions & 1 deletion src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@ def merge( arrA, arrB ):
merged_arr = [0] * elements
# TO-DO

for i in range(len(merged_arr)):

if len(arrA) > 0 and len(arrB) > 0:
# arryA[0] is smaller, add it to merged_arr and delete from arrA
if arrA[0] < arrB[0]:
merged_arr[i] = arrA[0]
arrA.remove(arrA[0])
else:
# arryB[0] is smaller, add it to merged_arr and delete from arrB
merged_arr[i] = arrB[0]
arrB.remove(arrB[0])
else:
# lenght of ArrA is equal to 0 merged_arr = arrB remove arrB. Else: merged_arr = arrA remove ArrA
if len(arrA) == 0:
merged_arr[i] = arrB[0]
arrB.remove(arrB[0])
else:
merged_arr[i] = arrA[0]
arrA.remove(arrA[0])

return merged_arr


# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO


if len(arr) > 1:
# divide list (array) and assign each half
middle = len(arr)//2
l, r = merge_sort(arr[:middle]), merge_sort(arr[middle:])
arr = merge(l, r)

return arr


Expand Down