Last active
December 31, 2024 03:59
-
-
Save msyvr/2341352428fd43283ad760ecf5ec0cd0 to your computer and use it in GitHub Desktop.
mit 6.0001 - selection sort
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
import random | |
def selection_sorted_list(ulist): | |
''' | |
sort a list recursively by shifting the minimum value of the portion of the | |
list to the right of a comparator index to the comparator index; the index | |
increases by 1 on each pass up to len(list) | |
''' | |
uoindex = 0 | |
while uoindex != len(ulist): | |
for i in range(uoindex, len(ulist)): | |
if ulist[i] < ulist[uoindex]: | |
ulist[uoindex], ulist[i] = ulist[i], ulist[uoindex] | |
uoindex += 1 | |
return ulist | |
if __name__ == "__main__": | |
ulist = random.sample(range(0,100), 10) | |
print(f'List to sort: {ulist}') | |
print(f'Selection-sorted list: {selection_sorted_list(ulist)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment