Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. #David Crowley - a17.py
  2. def Print( data ) :
  3.  
  4.     # Outputs the list 'data' of (name, age) tuples in the form
  5.     # of a headed table
  6.    
  7.     print( "%-7s %3s\n%-7s %3s" % ( "Name", "Age", "-"*4, "-"*3 ) )
  8.     for i in data :
  9.         print( "%-7s %3i" % ( i[0], i[1]) )
  10.  
  11. def InsertionSort( data, field ) :
  12.  
  13.     # Sort's the list 'data' of (name,age) tuples into ascending order
  14.     # of field 'field', which is either "name" or "age"
  15.     # Using the Insertion-Sort-Algorithm
  16.  
  17.     if field == "name":
  18.         x = 0
  19.     elif field == "age":
  20.         x = 1
  21.     else:
  22.         return None
  23.    
  24.     for i in range( len( data ) ) :
  25.         j = i
  26.         while j > 0 and data[j-1][x] > data[j][x] :
  27.             ( data[j], data[j-1] ) = ( data[j-1], data[j] )
  28.             j = j - 1
  29.  
  30. def SelectionSort( data, field) :
  31.  
  32.     # Sort's the list 'data' of (name,age) tuples into ascending order
  33.     # of field 'field', which is either "name" or "age"
  34.     # Using the Selection-Sort-algorithm
  35.    
  36.     if field == "name" :
  37.         x = 0
  38.     elif field == "age" :
  39.         x = 1
  40.     else:
  41.         return None
  42.  
  43.     for i in range( len( data ) ) :
  44.         imin = i
  45.         for j in range( i+ 1, len( data ) ) :
  46.             if data[j][x] < data[imin][x] :
  47.                 imin = j
  48.         ( data[i], data[imin] = data[imin], data[i] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement