First Summer 2008


Lecture Twelve: More about sorting and methods.
Today we will write a procedure to find the maximum in a list of elements:
def max(a):
    max = a[0]
    for i in range(len(a)):
        if a[i] > max:
            max = a[i]
    return max
We will then use that to sort by selection:
def max(a):
    max = a[0]
    for i in range(len(a)):
        if a[i] > max:
            max = a[i]
    return max

def sort(a):
    sorted = []
    for i in range(len(a)):
        number = min(a)
        sorted.append(number)
        a.remove(number)
    return sorted
This last method works as follows:
>>> numbers = [3, 11, 7, 5, 12, 2]
>>> sort(numbers)
[2, 3, 5, 7, 11, 12]
>>> numbers
[]
>>>
So it needs to be used as follows:
numbers = sort(numbers)
What other ways of sorting might we be able to work out?

The answer will be provided in the lab, where we will also try to offer help on Homework Four due tomorrow.


Last updated: May 21, 2008 by Adrian German for A201/A597