วันจันทร์ที่ 26 ตุลาคม พ.ศ. 2558

Lab07 : Insertion sort

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class student:
    def __init__(self,id_num,name,age,weight,height):
        self.id_num=id_num
        self.name=name
        self.age=age
        self.weight=weight
        self.height=height
    def display(self):
        print("id:",self.id_num)
        print("name:",self.name)
        print("age:",self.age)
        print("weight:",self.weight)
        print("height:",self.height)
    def getWeight(self):
        return self.weight
    def getHeight(self):
        return self.height
    def getAge(self):
        return self.age
    def getID(self):
        return self.id_num
    def getName(self):
        return self.name
    def setWeight(self,w):
        self.weight=w
    def setHeight(self,h):
        self.height=h
    def setAge(self,age):
        self.age=age
    def setID(self,id):
        self.id_num=id
    def setName(self,name):
        self.name=name


def setup():
    a=student(1,"John",18,100,160)
    b=student(2,"Jo",20,50,180)
    c=student(3,"Jan",17,42,150)
    d=student(4,"ISH",19,55,170)
    stdnt=[a,b,c,d]
    insertion_sort_class(stdnt) #sort by age
    display_class(stdnt)

    
def display_class(stdnt):
    i=0
    while i<len(stdnt): #display
        stdnt[i].display()
        i+=1
def insertion_sort_class(stdnt): 
    i=1
    j=0
    while i<len(stdnt):
        j=i
        while j>0 and stdnt[j].getAge()<stdnt[j-1].getAge():#Age
            swap(j,j-1,stdnt)
            j-=1
        i+=1
def swap(i,k,stdnt): 
    temp=stdnt[i].getAge() #1
    stdnt[i].setAge(stdnt[k].getAge())
    stdnt[k].setAge(temp) 
    temp=stdnt[i].getWeight() #2
    stdnt[i].setWeight(stdnt[k].getWeight())
    stdnt[k].setWeight(temp) 
    temp=stdnt[i].getHeight() #3
    stdnt[i].setHeight(stdnt[k].getHeight())
    stdnt[k].setHeight(temp) 
    temp=stdnt[i].getID() #4
    stdnt[i].setID(stdnt[k].getID())
    stdnt[k].setID(temp) 
    temp=stdnt[i].getName() #5
    stdnt[i].setName(stdnt[k].getName())
    stdnt[k].setName(temp) 
setup()

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

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

commit เพิ่มเติม : เพื่ม userstory, bookmark

>>  add userstory ทำการเพิ่ม userstory ใน functional tests >>  add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...