1
- '''
1
+ """
2
2
Created on 22 de dez de 2016
3
3
4
4
@author: patterson
5
- '''
5
+ """
6
+
6
7
import math
7
8
8
9
@@ -24,6 +25,7 @@ def bubble(nlist):
24
25
exchanges = True
25
26
26
27
return nlist
28
+
27
29
28
30
def insertion (nlist ):
29
31
nlist = list (nlist )
@@ -43,6 +45,7 @@ def insertion(nlist):
43
45
44
46
return nlist
45
47
48
+
46
49
def selection (nlist ):
47
50
nlist = list (nlist )
48
51
size = len (nlist )
@@ -59,6 +62,7 @@ def selection(nlist):
59
62
nlist [i ], nlist [lower ] = nlist [lower ], nlist [i ]
60
63
61
64
return nlist
65
+
62
66
63
67
def quick (nlist ):
64
68
nlist = list (nlist )
@@ -68,19 +72,20 @@ def quick(nlist):
68
72
return nlist
69
73
70
74
pivot = nlist [0 ]
71
- lr = [x for x in nlist if x < pivot ] # lower
72
- gt = [x for x in nlist [1 :] if x >= pivot ] # greater
75
+ lr = [x for x in nlist if x < pivot ] # lower
76
+ gt = [x for x in nlist [1 :] if x >= pivot ] # greater
73
77
74
78
return quick (lr ) + [pivot ] + quick (gt )
75
79
80
+
76
81
def merge (nlist ):
77
82
if len (nlist ) < 2 :
78
83
return nlist
79
84
80
85
result , mid = list (), len (nlist ) // 2
81
86
82
- l = merge (nlist [:mid ]) # left
83
- r = merge (nlist [mid :]) # right
87
+ l = merge (nlist [:mid ]) # left
88
+ r = merge (nlist [mid :]) # right
84
89
85
90
while (len (l ) > 0 ) and (len (r ) > 0 ):
86
91
if l [0 ] > r [0 ]:
@@ -92,6 +97,7 @@ def merge(nlist):
92
97
93
98
return result
94
99
100
+
95
101
def shell (nlist ):
96
102
nlist = list (nlist )
97
103
size = len (nlist )
@@ -115,6 +121,7 @@ def shell(nlist):
115
121
116
122
return nlist
117
123
124
+
118
125
def heap (nlist ):
119
126
nlist = list (nlist )
120
127
size = len (nlist )
@@ -131,6 +138,7 @@ def heap(nlist):
131
138
132
139
return nlist
133
140
141
+
134
142
def _max_heap (nlist , root , end ):
135
143
while True :
136
144
child = root * 2 + 1
@@ -144,7 +152,8 @@ def _max_heap(nlist, root, end):
144
152
root = child
145
153
else :
146
154
break
147
-
155
+
156
+
148
157
def counting (alist ):
149
158
nlist = list (alist )
150
159
size = len (nlist )
@@ -169,6 +178,7 @@ def counting(alist):
169
178
170
179
return nlist
171
180
181
+
172
182
def radix (aList ):
173
183
nlist = list (aList )
174
184
size = len (nlist )
@@ -178,15 +188,15 @@ def radix(aList):
178
188
179
189
RADIX = 10
180
190
maxLength = False
181
- tmp , placement = - 1 , 1
191
+ tmp , placement = - 1 , 1
182
192
183
193
while not maxLength :
184
194
maxLength = True
185
- buckets = [list () for i in range ( RADIX )]
195
+ buckets = [list () for i in range (RADIX )]
186
196
187
- for i in nlist :
197
+ for i in nlist :
188
198
tmp = int (i / placement )
189
- buckets [int (tmp % RADIX )].append ( i )
199
+ buckets [int (tmp % RADIX )].append (i )
190
200
if maxLength and tmp > 0 :
191
201
maxLength = False
192
202
@@ -201,6 +211,7 @@ def radix(aList):
201
211
202
212
return nlist
203
213
214
+
204
215
def bucket (aList , bucketSize = 5 ):
205
216
nlist = list (aList )
206
217
size = len (nlist )
@@ -228,6 +239,7 @@ def bucket(aList, bucketSize=5):
228
239
229
240
return nlist
230
241
242
+
231
243
def gnome (aList ):
232
244
nlist = list (aList )
233
245
size = len (nlist )
@@ -245,7 +257,8 @@ def gnome(aList):
245
257
pivot += 1
246
258
247
259
return nlist
248
-
260
+
261
+
249
262
def comb (aList ):
250
263
nlist = list (aList )
251
264
size = len (nlist )
@@ -268,7 +281,8 @@ def comb(aList):
268
281
swaps = True
269
282
270
283
return nlist
271
-
284
+
285
+
272
286
def cocktail (aList ):
273
287
nlist = list (aList )
274
288
size = len (nlist )
@@ -298,19 +312,3 @@ def cocktail(aList):
298
312
299
313
if not swapped :
300
314
return nlist
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
0 commit comments