"""File: ins_sort.py Purpose: Implement insertion sort on a list of ints Run: python ins_sort.py Input: A list of positive ints followed by a single int that's <= 0 Output: The updated list after each int has been inserted """ def ins_in_list(val, l): """Insert val into the list l so that l remains sorted Precondition: l is empty or sorted. Postcondition: l is sorted and val is in l """ l.append(val) i = len(l)-1 while i > 0 and l[i-1] > val: l[i] = l[i-1] i = i-1 l[i] = val #---------------------------------------------------------------------- # Main program starts here l = [] val = int(raw_input("Enter a positive int (<= 0 to stop)\n ")) while val > 0: ins_in_list(val, l) print l val = int(raw_input("Enter a positive int (<= 0 to stop)\n "))