Skip to content

finished iterative_sorting #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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