วันอาทิตย์ที่ 25 ตุลาคม พ.ศ. 2558

Display student records, sorted by age, use insertion sort

def setup():
   name=['aa','bb','cc','dd','ee','ff']
   ID=[54321,51234,52341,53412,54123,54132]
   age=[9,10,15,16,8,9]
   weight=[45,55,40,50,70,60]
   height=[145,155,140,150,170,160]
   i=0
   sort(name,ID,age,weight,height)
 
   while(i<len(weight)):
      display(name,ID,age,weight,height,i)
      i += 1

def display(n,I,a,w,h,i):
   print('Name :',n[i])
   print("ID =",I[i])
   print("age =",a[i])
   print("weight =",w[i])
   print("height =",h[i])
   print('---------------------')
     
def sort(n,I,a,w,h):
   i=1
   while(i<len(a)):
      x_a = a[i]
      x_n = n[i]
      x_I = I[i]
      x_w = w[i]
      x_h = h[i]
      j = i
      while(j>0 and a[j-1] > x_a):
         a[j] = a[j-1]
         n[j] = n[j-1]
         I[j] = I[j-1]
         w[j] = w[j-1]
         h[j] = h[j-1]
         j = j - 1
      a[j] = x_a
      n[j] = x_n
      I[j] = x_I
      w[j] = x_w
      h[j] = x_h
      i += 1
     


 
setup()

ไม่มีความคิดเห็น:

แสดงความคิดเห็น