-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysort.py
351 lines (299 loc) · 9.93 KB
/
mysort.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# ===================Bubble Sort=====================================
# Sorts a sequence in ascending order using the bubble sort algorithm.
def bubbleSort( theSeq ):
n = len( theSeq )
for i in range( n-1 ):
for j in range( n-i-1 ):
if theSeq[j] > theSeq[j+1]:
tmp = theSeq[j]
theSeq[j] = theSeq[j+1]
theSeq[j+1] = tmp
print 'iter ' + str(i+1)
for ele in theSeq:
print ele,
print ''
# ====================Selection Sort====================================
# Sorts a sequence in ascending order using the selection sort algorithm
def selectionSort( theSeq ):
n = len( theSeq )
for i in range( n-1 ):
smallNdx = i
for j in range( i+1, n ):
if theSeq[j] < theSeq[smallNdx ]:
smallNdx = j
if smallNdx != i:
tmp = theSeq[i]
theSeq[i] = theSeq[smallNdx]
theSeq[smallNdx] = tmp
print 'iter ' + str(i+1)
for ele in theSeq:
print ele,
print ''
# ===================Insertion Sort======================================
# Sorts a sequence in ascending order using the insertion sort algorithm.
def insertionSort( theSeq ):
n = len( theSeq )
for i in range(1, n):
value = theSeq[i]
pos = i
while pos > 0 and value < theSeq[pos-1]:
theSeq[pos] = theSeq[pos-1]
pos -= 1
theSeq[pos] = value
print 'iter ' + str(i)
for j in range(i+1):
print theSeq[j],
print ''
# ===================Merge Sort 01=======================================
# Merges two sorted lists to create and return a new sorted list.
def mergeSortedLists( listA, listB ):
newList = list()
a = 0
b = 0
# Merge the two lists together until one is empty
while a < len( listA ) and b < len( listB ):
if listA[a] < listB[b]:
newList.append( listA[a] )
a += 1
else:
newList.append( listB[b] )
b += 1
# If listA contains more items, append them to newList.
while a < len( listA ):
newList.append( listA[a] )
a += 1
# Or if listB contains more items, append them to newList.
while b < len( listB ):
newList.append( listB[b] )
b += 1
return newList
# Sorts a Python list in ascending order using the merge sort algorithm.
def MergeSort( theList ):
# Check the base case - the list contains s single item.
if len( theList ) <= 1:
return theList
else:
mid = len( theList ) // 2
leftHalf = MergeSort( theList[ :mid ] )
rightHalf = MergeSort( theList[ mid: ] )
newList = mergeSortedLists( leftHalf, rightHalf )
for ele in newList:
print ele,
print ''
return newList
# ========================Merge Sort 02==================================
# Sorts a virtual subsequence in ascending order using merge sort.
def recMergeSort( theSeq, first, last, tmpArray ):
if first == last:
return
else:
mid = ( first + last ) // 2
recMergeSort( theSeq, first, mid, tmpArray )
recMergeSort( theSeq, mid+1, last, tmpArray )
mergeVirtualSeq( theSeq, first, mid+1, last+1, tmpArray )
for ele in tmpArray:
print ele,
print ''
# Merges the two sorted virtual sublists: [left...right) [right..end)
def mergeVirtualSeq( theSeq, left, right, end, tmpArray ):
a = left
b = right
m = 0
while a < right and b < end:
if theSeq[a] < theSeq[b]:
tmpArray[m] = theSeq[a]
a += 1
else:
tmpArray[m] = theSeq[b]
b += 1
m += 1
while a < right:
tmpArray[m] = theSeq[a]
a += 1
m += 1
while b < end:
tmpArray[m] = theSeq[b]
b += 1
m += 1
for i in range( end - left ):
theSeq[i+left] = tmpArray[i]
# a wrapper function for the virtual subsequence merge sort
def mergeVirtualSort( theSeq ):
from myarray import myArray
n = len( theSeq )
tmpArray = myArray(n)
recMergeSort( theSeq, 0, n-1, tmpArray )
# ===========================Quick Sort===============================
# Sorts an array or list using the recursive quick sort algorithm
def quickSort( theSeq ):
n = len( theSeq )
recQuickSort( theSeq, 0, n-1 )
# The recursive implementation using virtual segments.
def recQuickSort( theSeq, first, last ):
if first >= last:
#for i in range(len(theSeq)):
# print theSeq[i],
#print ''
return
else:
#pivot = theSeq[first]
pos = partitionSeq( theSeq, first, last )
for i in range(first, pos):
print theSeq[i],
print ''
print theSeq[pos]
for i in range(pos+1, last+1):
print theSeq[i],
print ''
print '======='
recQuickSort( theSeq, first, pos-1 )
recQuickSort( theSeq, pos+1, last )
def partitionSeq( theSeq, first, last ):
pivot = theSeq[first]
print 'current pivot: %d' % pivot
left = first + 1
right = last
#print left, right
while left <= right:
# Find the first key larger than the pivot
while left < right and theSeq[left] < pivot:
left += 1
# Find the last key smaller than the pivot
while right >= left and theSeq[right] >= pivot:
right -= 1
# Swap the two keys
if left < right:
tmp = theSeq[left]
theSeq[left] = theSeq[right]
theSeq[right] = tmp
elif left == right:
break
if right != first:
theSeq[first] = theSeq[right]
theSeq[right] = pivot
return right
# =======================heap sort 01==================================
def simpleHeapSort( theSeq ):
from arrayheap import MaxHeap
# Create an array-based max-heap.
n = len( theSeq )
heap = MaxHeap( n )
# Build a max-heap from the list of values.
for item in theSeq:
heap.add( item )
# Extract each value from the heap and store them back into the list.
for i in range(n)[::-1]:
theSeq[i] = heap.extract()
# =======================heap sort 02==================================
# heap sort in place
def heapSort( theSeq ):
n = len( theSeq )
# Build a max-heap within the same array.
for i in range( n ):
_siftUp( theSeq, i )
for i in range( n ):
print theSeq[i],
print ''
# Extract each value and rebuild the heap.
for j in range( n-1, 0, -1 ):
tmp = theSeq[ j ]
theSeq[j] = theSeq[0]
theSeq[0] = tmp
_siftDown( theSeq, j-1, 0 )
for i in range( n ):
print theSeq[i],
print ''
# help function used in heapsort
def _siftUp( theSeq, ndx ):
if ndx > 0:
parent = ( ndx - 1 ) // 2
if theSeq[ndx] > theSeq[parent]:
tmp = theSeq[ndx]
theSeq[ndx] = theSeq[parent]
theSeq[parent] = tmp
_siftUp( theSeq, parent )
# help function used in heapsort
def _siftDown( theSeq, end_ndx, begin_ndx ):
left = 2 * begin_ndx + 1
right = 2 * begin_ndx + 2
largest = begin_ndx
if left <= end_ndx and theSeq[left] >= theSeq[largest]:
largest = left
if right <= end_ndx and theSeq[right] >= theSeq[largest]:
largest = right
if largest != begin_ndx:
tmp = theSeq[begin_ndx]
theSeq[begin_ndx] = theSeq[largest]
theSeq[largest] = tmp
_siftDown( theSeq, end_ndx, largest )
# =======================radix sort====================================
# Sorts a sequence of positive integers using the radix sort algorithm.
def radixSort( intList, numDigits ):
from llistqueue import Queue
from myarray import myArray
# Create an array of queues to represent the bins.
binArray = myArray( 10 )
for k in range( 10 ):
binArray[k] = Queue()
column = 1
# Iterate over the number of digits in the largest value.
for d in range( numDigits ):
# Distribute the keys across the 10 bins.
for key in intList:
digit = (key // column) % 10
binArray[digit].enqueue( key )
# Gather the keys from the bins and place them back in intList.
i = 0
for bin in binArray:
while not bin.isEmpty():
intList[i] = bin.dequeue()
i += 1
# Advance to the next column value.
column *= 10
for ele in intList:
print ele,
print ''
# =======================module test===================================
if __name__ == '__main__':
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Bubble Sort: '
#bubbleSort(theSeq)
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Selection Sort: '
#selectionSort(theSeq)
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Insertion Sort: '
#insertionSort(theSeq)
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Merge Sort: '
#sortedSeq = MergeSort( theSeq )
#for ele in sortedSeq:
# print ele,
#print ''
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Merge Sort ( using virtual subsequence )'
#mergeVirtualSort( theSeq )
#for ele in theSeq:
# print ele,
#print ''
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Quick Sort: '
#quickSort( theSeq )
#for ele in theSeq:
# print ele,
#print ''
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Heap Sort: '
#simpleHeapSort( theSeq )
#for ele in theSeq:
# print ele,
#print ''
theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
print 'Heap Sort in place: '
heapSort( theSeq )
for ele in theSeq:
print ele,
print ''
#theSeq = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
#print 'Radix Sort: '
#radixSort( theSeq, 2 )