Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
'''Selection Sort
input: unsorted list
output: sorted list
Implements Selection sort with in-place swap
'''
aList = [1,3,6,7,3,8,5,3,11,4,8,4,6,8,4] #list to be sorted
def selectionSort(aList):
for i in range(len(aList)): #checks each value depending on size of list
minimum = i #set the minimum value as each value read from the list
for num in range(i+1,len(aList)): #compares numbers after the one currently being checked
if aList[minimum] > aList[num]: #if the minimum value is larger, it cant be the minimum..
minimum = num #..therefore set the smaller value as the new minimum
aList[i], aList[minimum] = aList[minimum], aList[i] #swap the smaller and larger values
return aList
print(aList) #before the function runs, unsorted
selectionSort(aList)
print("sorted list: ", aList) #after the function runs, sorted