Skip to content

Instantly share code, notes, and snippets.

@ombak
Last active May 31, 2020 16:21
Show Gist options
  • Save ombak/52f7edd2eb4277569ee37cd1ecf9684e to your computer and use it in GitHub Desktop.
Save ombak/52f7edd2eb4277569ee37cd1ecf9684e to your computer and use it in GitHub Desktop.
def selection_sort(mylist):
# i indicates how many items were sorted
for i in range(len(mylist)-1):
# To find the minimum value of the unsorted segment
# we first assume that the first element is the lowest
min_index = i
# We then use j to loop through the remaining elements
dex = 0
for j in range(i+1, len(mylist)):
if mylist[j] < mylist[min_index]:
min_index = j
# After finding the lowest item of the unsorted regions, swap with the first unsorted item
mylist[i], mylist[min_index] = mylist[min_index], mylist[i]
return mylist
thelist = [3, 4, 1, 6, 2]
print(thelist)
print(selection_sort(thelist))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment