|
First Summer 2008 |
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:
So it needs to be used as follows:>>> numbers = [3, 11, 7, 5, 12, 2] >>> sort(numbers) [2, 3, 5, 7, 11, 12] >>> numbers [] >>>
What other ways of sorting might we be able to work out?numbers = sort(numbers)
The answer will be provided in the lab, where we will also try to offer help on Homework Four due tomorrow.