Permalink
Cannot retrieve contributors at this time
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?
5003CEM_Tasks/STD_1 - Selection Sort.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
21 lines (17 sloc)
911 Bytes
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
'''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 |