"""File: sel_sort4.py Purpose: Implements a sorting function for lists Run: python sel_sort4.py Input: number of ints in list the ints in the list Output: unsorted and sorted list Note: This is a modification of sel_sort3.py: we've eliminated the final (unnecessary) pass through the main for loop. """ def get_ints(l, n): """Get n ints from the user and return them in the list l""" print "Enter", n ,"ints (all on the same line)" print " ", s = raw_input("") lc = s.split() # Split s into substrings for c in lc: x = int(c) l.append(x) # Add x to the end of l def sort_list(l): """Reorder the elements of l so that they're increasing order""" for i in range(len(l)-1): e = l[i] eloc = i for j in range(i+1, len(l)): if l[j] < e: e = l[j] eloc = j temp = l[i] l[i] = l[eloc] l[eloc] = temp # print "After i =", i, "e = ", e, ", list =", l #---------------------------------------------------------------------- # Main program starts here n = int(raw_input("How many ints?\n ")) l = [] get_ints(l, n) print "Before sort l =", l sort_list(l) print "After sort l =", l