| 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
| from Tkinter import *
import random
import time
canheight = 500
canwidth = 300
listlength = 20
listmax = 50
lmargin = 10
umargin = 10
spacer = 2
def makelist(length, max):
tmp = []
for i in range(length):
x = random.randint(0,max)
tmp.append([x,can.create_rectangle(lmargin,umargin + (i+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(x*float((canwidth-2*lmargin))/float(listmax)),(i+1)*(float(canheight-2*umargin)/float(listlength)),width=0,fill="blue")])
return tmp
def insert(list,num):
i = 0
def insertionsort(n):
i = 0
while i < len(n):
can.itemconfig(n[i][1],fill="green")
can.update()
time.sleep(0.25)
j = 0
inserted = 0
while j < i and not inserted:
if n[i][0] < n[j][0]:
n.insert(j,n[i])
inserted = 1
j += 1
if not inserted:
n.insert(j,n[i])
del n[i+1]
i += 1
for l in range(len(n)):
can.coords(n[l][1],lmargin,umargin + (l+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(n[l][0]*float((canwidth-2*lmargin))/float(listmax)),(l+1)*(float(canheight-2*umargin)/float(listlength)))
can.update()
time.sleep(0.25)
for m in range(i):
can.itemconfig(n[i][1],fill="red")
can.update()
time.sleep(0.5)
return n
def merge(n1,n2):
for i in range(len(n1)):
can.itemconfig(n1[i][1],fill = "green")
for i in range(len(n1)):
can.itemconfig(n2[i][1],fill = "orange")
can.update()
time.sleep(1)
tmp = []
while len(n1) + len(n2) > 0:
if len(n1) == 0:
tmp = tmp + n2
del n2[:]
elif len(n2) == 0:
tmp = tmp + n1
del n1[:]
else:
if n1[0][0] < n2[0][0]:
tmp.append(n1[0])
del n1[0]
else:
tmp.append(n2[0])
del n2[0]
for i in range(len(tmp)):
can.itemconfig(tmp[i][1],fill="green")
can.update()
return tmp
def mergesort(n,pos):
for i in range(len(n)):
can.coords(n[i][1],lmargin,umargin + (i+pos+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(n[i][0]*float((canwidth-2*lmargin))/float(listmax)),(i+pos+1)*(float(canheight-2*umargin)/float(listlength)))
can.update()
if len(n)>1:
n1 = n[:len(n)/2]
n2 = n[len(n)/2:]
n1 = mergesort(n1,pos)
n2 = mergesort(n2,pos+len(n)/2)
for i in range(len(n1)):
can.coords(n1[i][1],lmargin,umargin + (i+pos+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(n1[i][0]*float((canwidth-2*lmargin))/float(listmax)),(i+pos+1)*(float(canheight-2*umargin)/float(listlength)))
for i in range(len(n2)):
can.coords(n2[i][1],lmargin,umargin + (i+pos+len(n)/2+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(n2[i][0]*float((canwidth-2*lmargin))/float(listmax)),(i+pos+len(n)/2+1)*(float(canheight-2*umargin)/float(listlength)))
can.update()
time.sleep(0.05)
return merge(n1,n2)
else:
return n
def bubblesort(n):
for i in range(len(n)):
j = len(n)-1
while j > i:
can.itemconfig(n[j][1],fill = "green")
can.itemconfig(n[j-1][1],fill = "green")
can.update()
time.sleep(0.05)
if n[j][0] < n[j-1][0]:
tmp = n[j]
n[j] = n[j-1]
n[j-1] = tmp
can.itemconfig(n[j][1],fill = "blue")
can.itemconfig(n[j-1][1],fill = "blue")
for l in range(len(n)):
can.coords(n[l][1],lmargin,umargin + (l+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(n[l][0]*float((canwidth-2*lmargin))/float(listmax)),(l+1)*(float(canheight-2*umargin)/float(listlength)))
can.update()
j -= 1
return n
canheight = 500
canwidth = 300
listlength = 20
listmax = 50
lmargin = 10
umargin = 10
spacer = 2
win = Tk()
can = Canvas(win,height=canheight, width = canwidth, background="white")
can.pack()
a = makelist(listlength, listmax)
time.sleep(0.5)
a = mergesort(a,0)
#a = insertionsort(a)
#a = bubblesort(a)
for i in range(len(a)):
can.coords(a[i][1],lmargin,umargin + (i+1)*(float(canheight-2*umargin)/float(listlength)),lmargin+(a[i][0]*float((canwidth-2*lmargin))/float(listmax)),(i+1)*(float(canheight-2*umargin)/float(listlength)))
can.mainloop() |