Skip to content

Instantly share code, notes, and snippets.

@msyvr
Last active October 29, 2021 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msyvr/2341352428fd43283ad760ecf5ec0cd0 to your computer and use it in GitHub Desktop.
Save msyvr/2341352428fd43283ad760ecf5ec0cd0 to your computer and use it in GitHub Desktop.
mit 6.0001 - selection sort
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