Last active
August 29, 2020 14:17
-
-
Save shakeelmajeed-work/47a662b65f55b8895fe98dccb1640b5b to your computer and use it in GitHub Desktop.
Selection Sort in Python 3!
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
Array = [18, 6, 66, 44, 9, 22, 14] #example | |
for i in range(len(Array)): #looping through till length of array | |
minindex = i #initial index of smallest element | |
''''whilst looping through whole array, find the minimum value''' | |
for j in range(i+1, len(Array)): | |
if Array[minindex]>Array[j]: #if value at minimum index is bigger than the value adjancent to it | |
minindex=j #change the minimum index | |
#after finding the smallest element, swap it with the first index(increases by 1 due to the first loop) | |
Array[i], Array[minindex] = Array[minindex], Array[i] | |
#step by step output | |
print(Array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment