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
33 changes: 21 additions & 12 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
# TO-DO: Complete the selection_sort() function below
def selection_sort( arr ):
# loop through n-1 elements
# loop through zero to len(arr)-1 elements bc the last will end up being the highest number. When you get to it you'll return arr
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


cur_index = i #pivot set to temp
#loop through again comparing the pivot to the other items in the array
for j in range(i+1, len(arr)):
#if a number in the array is less than the pivot
if arr[j] < arr[cur_index]:
#reset the pivot to the number
cur_index = j
#swap the order of the sorted array is the number is less than the pivot
arr[i], arr[cur_index] = arr[cur_index], arr[i]


return arr


# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):
swap = True
while swap:
swap = False #while running in the loop set swap to false
#loop through zero to len(arr)-1 elements bc the last will end up being the highest number. When you get to it you'll return arr
for n in range(len(arr)-1):
#if the pivot is greater than the item next to it in the array
if arr[n] > arr[n+1]:
#change positions with number and pivot
arr[n], arr[n+1] = arr[n+1], arr[n]
#reset swap to start the comparison loop over
swap = True

return arr

Expand Down
23 changes: 23 additions & 0 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,35 @@ def merge( arrA, arrB ):
merged_arr = [0] * elements
# TO-DO

#the arrays are pre-sorted
#compare item zero in the first two arrays and append a new array
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
if(len(arr) > 1):
left = merge_sort(arr[ : len(arr)//2])
right = merge_sort(arr[len(arr)//2 : ])
arr = merge(left, right)

return arr

Expand Down