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
25 changes: 15 additions & 10 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
# TO-DO: Complete the selection_sort() function below
def selection_sort( arr ):
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)



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



if cur_index != smallest_index:
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 ):

def bubble_sort(arr):
is_swapped = True
while is_swapped:
is_swapped = False
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
is_swapped = True
return arr


Expand Down
27 changes: 25 additions & 2 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@ def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO


a = 0
b = 0

for i in range (0, elements):
if a >= len(arrA):
merged_arr[i] = arrB[b]
b += 1
elif b >= len(arrB):
merged_arr[i] = arrA[a]
a += 1
elif arrA[a] < arrB[b]:
merged_arr[i] = arrA[a]
a += 1
else:
merged_arr[i] = arrB[b]
b += 1
return merged_arr


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

return arr
n = len(arr)
if n < 2:
return arr

mid = n // 2
arr_l = arr[:mid]
arr_r = arr[mid:]

return merge(merge_sort(arr_l), merge_sort(arr_r))

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