
def selection_sort(lst):
    '''
    Purpose: Sort a list using selection sort.
    Pre:  lst is a list, with elements all of the same type.
    Post: Mutates the list to be sorted in ascending order.
          Returns nothing.
    '''

    # For each index except for the last.
    # Guarantee: Before iteration i, the i smallest elements are
    #   already in the correct place (indices 0 through i-1).
    #   After iteration i, the i+1 smallest elements are in the
    #   correct place (indices 0 through i).
    for i in range(len(lst) - 1):
        # Find the smallest element in the unsorted part of the
        # list (starting at index i):
        small = min(lst[i:])

        # Only searching the unsorted part, so if there are duplicates
        # we don't find the one we already sorted.
        pos = lst.index(small, i)

        # Swap that element to index i
        lst[i], lst[pos] = lst[pos], lst[i]

        # For debugging and demonstration purposes only:
        print("After iteration", i, ":", lst)

    # After the loop is over, the (len(lst) - 1) smallest elements
    # are in the correct place.  But that means there is only one
    # spot remaining for the largest element, so it must be in the
    # right place, too.

def main():
    '''Driver program to test selection_sort.'''
    # Accumulator for user input.
    numbers = []

    num = int(input("Enter an integer, -1 to finish and sort: "))
    while num != -1:
        numbers.append(num)
        num = int(input("Enter an integer, -1 to finish and sort: "))

    print("Original:", numbers)

    selection_sort(numbers)

    print("Sorted:", numbers)

main()
