# sorts a list of numbers in increasing order # ECS 10, Winter 2012 # Matt Bishop, March 10, 2012 mylist = [ 1, 20, 3, -4, 8, 0, 6] # do a selection sort over the list # parameters: lst, the list of numbers to sort # returns: nothing def selsort(lst): # go through the list # each (outer) loop puts the smallest number # at the head of the list beginning with the # given index n = len(lst) for i in range(n-1): # assume smallest is already at head of list mp = i # look for a smaller number in the rest of the list for j in range(i+1, n): # if it's smaller, remember where it is if lst[mp] > lst[j]: mp = j # now swap the smallest to the head lst[i], lst[mp] = lst[mp], lst[i] # this puts it all together def main(): # sort and print the list selsort(mylist) print(mylist) main()