Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
608 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'''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 |
Oops, something went wrong.