-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortAlgorithm.py
159 lines (140 loc) · 3.55 KB
/
SortAlgorithm.py
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
148
149
150
151
152
153
154
155
156
157
158
159
import RandData
import time
#Bubble Sort
def Bubble_Sort(list):
TL = len(list)-2
for k in range(len(list)):
for i in range(TL+1):
j = i+1
if list[i] >= list[j]:
list[i], list[j] = list[j], list[i]
return list
#Insert Sort
def Insert_Sort(list):
for i in range(len(list)-1):
for j in list[:i+1]:
if list[i+1] <= j:
list.insert(list.index(j), list.pop(i+1))
break
return list
#Shell Sort
def Shell_Sort(list):
global i
LN = len(list)/2
while LN > 0:
for i in range(LN, len(list)):
while i >= LN:
if list[i-LN] >= list[i]:
list[i-LN], list[i] = list[i], list[i-LN]
i -= LN
LN = LN/2
return list
#Merge Sort
def Merge_Sort(list):
def DivideSort(list):
if len(list) <= 1:
return list
num = len(list)/2
Left = Merge_Sort(list[:num])
Right = Merge_Sort(list[num:])
return Merge(Left, Right)
def Merge(Left, Right):
NewSpace = []
while len(Left) > 0 and len(Right) > 0:
if Left[0] <= Right[0]:
NewSpace.append(Left[0])
Left.pop(0)
elif Left[0] > Right[0]:
NewSpace.append(Right[0])
Right.pop(0)
if len(Left) > 0:
for i in Left:
NewSpace.append(i)
elif len(Right) > 0:
for i in Right:
NewSpace.append(i)
return NewSpace
return DivideSort(list)
#Heap Sort
def Heap_Sort(list):
def Heap(list):
if (len(list) - 1) % 2 == 0:
num = len(list) / 2 - 1
for i in range(num, -1, -1):
if list[2 * i + 1] == min(list[i], list[2 * i + 1], list[2 * i + 2]):
list[i], list[2 * i + 1] = list[2 * i + 1], list[i]
elif list[2 * i + 2] == min(list[i], list[2 * i + 1], list[2 * i + 2]):
list[i], list[2 * i + 2] = list[2 * i + 2], list[i]
else:
continue
else:
num = (len(list)-1) / 2
for i in range(num, -1, -1):
if i < num:
if list[2 * i + 1] == min(list[i], list[2 * i + 1], list[2 * i + 2]):
list[i], list[2 * i + 1] = list[2 * i + 1], list[i]
elif list[2 * i + 2] == min(list[i], list[2 * i + 1], list[2 * i + 2]):
list[i], list[2 * i + 2] = list[2 * i + 2], list[i]
else:
continue
else:
if list[i] <= list[2 * i + 1]:
continue
else:
list[i], list[2 * i + 1] = list[2 * i + 1], list[i]
return list
def Sort(list):
for i in range(len(list)-2):
temp = Heap(list[i:])
list[i:] = temp
return list
list = Heap(list)
Sort(list)
return list
TestList = [2, 3, 5, 8, 90, 4, 2, 5, 0]
print '/nStart generate randam data...'
list=RandData.randata()
print 'Data generation finished.'
a = list
print '/nStart internal sort...'
t1=time.clock()
a.sort()
t2=time.clock()
print 'Internal sort finisehd. Time used=%fs'%(t2-t1)
print a
a = list
print '/nStart bubble sort...'
t1=time.clock()
Bubble_Sort(a)
t2=time.clock()
print 'Bubble sort finished. Time used=%fs'%(t2-t1)
print a
a = list
print '/nStart insert sort...'
t1=time.clock()
Insert_Sort(a)
t2=time.clock()
print 'Insert sort finished. Time used=%fs'%(t2-t1)
print a
a = list
print '/nStart shell sort...'
t1=time.clock()
Shell_Sort(a)
t2=time.clock()
print 'Shell sort finished. Time used=%fs'%(t2-t1)
print a
a = list
print '/nStart merge sort with new space...'
t1=time.clock()
Merge_Sort(a)
t2=time.clock()
print 'Merge sort with new space finished. Time used=%fs'%(t2-t1)
print a
a = list
print '/nStart heap sort without new space...'
t1=time.clock()
Heap_Sort(a)
t2=time.clock()
print 'Merge sort without new space finished. Time used=%fs'%(t2-t1)
print a
#print Heap_Sort(RandData.randata())