Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #David Crowley - a17.py
- def Print( data ) :
- # Outputs the list 'data' of (name, age) tuples in the form
- # of a headed table
- print( "%-7s %3s\n%-7s %3s" % ( "Name", "Age", "-"*4, "-"*3 ) )
- for i in data :
- print( "%-7s %3i" % ( i[0], i[1]) )
- def InsertionSort( data, field ) :
- # Sort's the list 'data' of (name,age) tuples into ascending order
- # of field 'field', which is either "name" or "age"
- # Using the Insertion-Sort-Algorithm
- if field == "name":
- x = 0
- elif field == "age":
- x = 1
- else:
- return None
- for i in range( len( data ) ) :
- j = i
- while j > 0 and data[j-1][x] > data[j][x] :
- ( data[j], data[j-1] ) = ( data[j-1], data[j] )
- j = j - 1
- def SelectionSort( data, field) :
- # Sort's the list 'data' of (name,age) tuples into ascending order
- # of field 'field', which is either "name" or "age"
- # Using the Selection-Sort-algorithm
- if field == "name" :
- x = 0
- elif field == "age" :
- x = 1
- else:
- return None
- for i in range( len( data ) ) :
- imin = i
- for j in range( i+ 1, len( data ) ) :
- if data[j][x] < data[imin][x] :
- imin = j
- ( data[i], data[imin] = data[imin], data[i] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement