Last active
May 31, 2020 16:21
-
-
Save ombak/52f7edd2eb4277569ee37cd1ecf9684e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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