Skip to content
Open
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
49 changes: 31 additions & 18 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
# 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




# loop through all elements
for i in range(len(arr)):
# flag to see if item i has been inserted in sorted array
replaced = False
# loop through the already sorted part of the list
for j in range(i+1):
# if item i has already been inserted
if replaced:
# move items to the right
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
elif arr[j] > arr[i]:
# if this is where the item should be inserted
# then insert it and save item to arr[i]
temp = arr[j]
arr[j] = arr[i]
arr[i] = temp
replaced = True
return arr


# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):
changes = True
while changes:
changes = False
for i in range(len(arr)-1):
j = i+1
if arr[i] > arr[j]:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
changes = True

return arr


# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum=-1 ):
# def count_sort( arr, maximum=-1 ):

return arr
# return arr