8000
tr>2
2
# Create by LokiSharp(loki.sharp#gmail) at 2017-1-22
3
3
'''
4
4
5
- TOTAL = 5000
5
+ TOTAL = 5000
6
+
6
7
7
8
def sortTest (func , total = 1000 ):
8
9
import random , copy , operator , math , time
9
- arrList = [i for i in range (- math .floor (total / 2 ),math .ceil (total / 2 ))]
10
+ arrList = [i for i in range (- math .floor (total / 2 ), math .ceil (total / 2 ))]
10
11
arrListR = copy .deepcopy (arrList )
11
- while operator .eq (arrList ,arrListR ):
12
+ while operator .eq (arrList , arrListR ):
12
13
random .shuffle (arrListR )
13
- #print("--- [Origin List]", arrList, "Use", func.__name__,"with Total:", len(arrList))
14
- #print("--> [Random List]", arrListR, "Use", func.__name__,"with Total:", len(arrList))
14
+ # print("--- [Origin List]", arrList, "Use", func.__name__,"with Total:", len(arrList))
15
+ # print("--> [Random List]", arrListR, "Use", func.__name__,"with Total:", len(arrList))
15
16
start = time .clock ()
16
17
arrListR = func (arrListR )
17
18
end = time .clock ()
18
- runtime = end - start
19
- #print("--> [Sorted List]", arrListR, "Use", func.__name__,"with Total:", len(arrList))
19
+ runtime = end - start
20
+ # print("--> [Sorted List]", arrListR, "Use", func.__name__,"with Total:", len(arrList))
20
21
if operator .eq (arrList , arrListR ):
21
- print ("[Success]" , func .__name__ ,"with Total:" , len (arrList ),"in %.5fs" % runtime )
22
+ print ("[Success]" , func .__name__ , "with Total:" , len (arrList ), "in %.5fs" % runtime )
22
23
return True
23
24
else :
24
- print ("[Fail]" , func .__name__ ,"with Total:" , len (arrList ),"in %.5fs" % runtime )
25
+ print ("[Fail]" , func .__name__ , "with Total:" , len (arrList ), "in %.5fs" % runtime )
25
26
return False
26
27
28
+
27
29
def bubbleSort (arr ):
28
30
for i in range (1 , len (arr )):
29
- for j in range (0 , len (arr )- i ):
30
- if arr [j ] > arr [j + 1 ]:
31
+ for j in range (0 , len (arr ) - i ):
32
+ if arr [j ] > arr [j + 1 ]:
31
33
arr [j ], arr [j + 1 ] = arr [j + 1 ], arr [j ]
32
34
return arr
33
35
36
+
34
37
def selectionSort (arr ):
35
- for i in range (len (arr )- 1 ):
36
- for j in range (i + 1 , len (arr )):
37
- if arr [j ] < arr [i ]:
38
- arr [i ], arr [j ] = arr [j ], arr [i ]
38
+ for i in range (len (arr ) - 1 ):
39
+ # 记录最小数的索引
40
+ minIndex = i
41
+ for j in range (i + 1 , len (arr )):
42
+ if arr [j ] < arr [minIndex ]:
43
+ minIndex = j
44
+ # i 不是最小数时,将 i 和最小数进行交换
45
+ if i != minIndex :
46
+ arr [i ], arr [minIndex ] = arr [minIndex ], arr [i ]
39
47
return arr
40
48
49
+
41
50
def insertionSort (arr ):
42
51
for i in range (len (arr )):
43
- preIndex = i - 1
52
+ preIndex = i - 1
44
53
current = arr [i ]
45
54
while preIndex >= 0 and arr [preIndex ] > current :
46
- arr [preIndex + 1 ] = arr [preIndex ]
47
- preIndex -= 1
48
- arr [preIndex + 1 ] = current
55
+ arr [preIndex + 1 ] = arr [preIndex ]
56
+ preIndex -= 1
57
+ arr [preIndex + 1 ] = current
49
58
return arr
50
59
60
+
51
61
def shellSort (arr ):
52
62
import math
53
- gap = 1
54
- while (gap < len (arr )/ 3 ):
55
- gap = gap * 3 + 1
63
+ gap = 1
64
+ while (gap < len (arr ) / 3 ):
65
+ gap = gap * 3 + 1
56
66
while gap > 0 :
57
- for i in range (gap ,len (arr )):
67
+ for i in range (gap , len (arr )):
58
68
temp = arr [i ]
59
- j = i - gap
60
- while j >= 0 and arr [j ] > temp :
61
- arr [j + gap ]= arr [j ]
62
- j -= gap
63
- arr [j + gap ] = temp
64
- gap = math .floor (gap / 3 )
69
+ j = i - gap
70
+ while j >= 0 and arr [j ] > temp :
71
+ arr [j + gap ] = arr [j ]
72
+ j -= gap
73
+ arr [j + gap ] = temp
74
+ gap = math .floor (gap / 3 )
65
75
return arr
66
76
77
+
67
78
def mergeSort (arr ):
68
79
import math
69
- if (len (arr )< 2 ):
80
+ if (len (arr ) < 2 ):
70
81
return arr
71
- middle = math .floor (len (arr )/ 2 )
82
+ middle = math .floor (len (arr ) / 2 )
72
83
left , right = arr [0 :middle ], arr [middle :]
73
84
return merge (mergeSort (left ), mergeSort (right ))
74
85
75
- def merge (left ,right ):
86
+
87
+ def merge (left , right ):
76
88
result = []
77
89
while left and right :
78
90
if left [0 ] <= right [0 ]:
@@ -85,38 +97,43 @@ def merge(left,right):
85
97
result .append (right .pop (0 ));
86
98
return result
87
99
100
+
88
101
def quickSort (arr , left = None , right = None ):
89
- left = 0 if not isinstance (left ,(int , float )) else left
90
- right = len (arr )- 1 if not isinstance (right ,(int , float )) else right
102
+ left = 0 if not isinstance (left , (int , float )) else left
103
+ right = len (arr ) - 1 if not isinstance (right , (int , float )) else right
91
104
if left < right :
92
105
partitionIndex = partition (arr , left , right )
93
- quickSort (arr , left , partitionIndex - 1 )
94
- quickSort (arr , partitionIndex + 1 , right )
106
+ quickSort (arr , left , partitionIndex - 1 )
107
+ quickSort (arr , partitionIndex + 1 , right )
95
108
return arr
96
109
110
+
97
111
def partition (arr , left , right ):
98
112
pivot = left
99
- index = pivot + 1
113
+ index = pivot + 1
100
114
i = index
101
- while i <= right :
115
+ while i <= right :
102
116
if arr [i ] < arr [pivot ]:
103
117
swap (arr , i , index )
104
- index += 1
105
- i += 1
106
- swap (arr ,pivot ,index - 1 )
107
- return index - 1
118
+ index += 1
119
+ i += 1
120
+ swap (arr , pivot , index - 1 )
121
+ return index - 1
122
+
108
123
109
124
def swap (arr , i , j ):
110
125
arr [i ], arr [j ] = arr [j ], arr [i ]
111
126
127
+
112
128
def buildMaxHeap (arr ):
113
129
import math
114
- for i in range (math .floor (len (arr )/ 2 ),- 1 ,- 1 ):
115
- heapify (arr ,i )
130
+ for i in range (math .floor (len (arr ) / 2 ), - 1 , - 1 ):
131
+ heapify (arr , i )
132
+
116
133
117
134
def heapify (arr , i ):
118
- left = 2 * i + 1
119
- right = 2 * i + 2
135
+ left = 2 * i + 1
136
+ right = 2 * i + 2
120
137
largest = i
121
138
if left < arrLen and arr [left ] > arr [largest ]:
122
139
largest = left
@@ -127,39 +144,44 @@ def heapify(arr, i):
127
144
swap (arr , i , largest )
128
145
heapify (arr , largest )
129
146
147
+
130
148
def swap (arr , i , j ):
131
149
arr [i ], arr [j ] = arr [j ], arr [i ]
132
150
151
+
133
152
def heapSort (arr ):
134
153
global arrLen
135
154
arrLen = len (arr )
136
155
buildMaxHeap (arr )
137
- for i in range (len (arr )- 1 , 0 , - 1 ):
138
- swap (arr ,0 , i )
139
- arrLen -= 1
156
+ for i in range (len (arr ) - 1 , 0 , - 1 ):
157
+ swap (arr , 0 , i )
158
+ arrLen -= 1
140
159
heapify (arr , 0 )
141
160
return arr
142
161
162
+
143
163
def countingSort (arr , maxValue = None ):
144
- bucketLen = maxValue + 1
145
- bucket = [0 ]* bucketLen
146
- sortedIndex = 0
164
+ bucketLen = maxValue + 1
165
+ bucket = [0 ] * bucketLen
166
+ sortedIndex = 0
147
167
arrLen = len (arr )
148
168
for i in range (arrLen ):
149
169
if not bucket [arr [i ]]:
150
- bucket [arr [i ]]= 0
151
- bucket [arr [i ]]+= 1
170
+ bucket [arr [i ]] = 0
171
+ bucket [arr [i ]] += 1
152
172
for j in range (bucketLen ):
153
- while bucket [j ]> 0 :
173
+ while bucket [j ] > 0 :
154
174
arr [sortedIndex ] = j
155
- sortedIndex += 1
156
- bucket [j ]-= 1
175
+ sortedIndex += 1
176
+ bucket [j ] -= 1
157
177
return arr
158
178
159
- sortTest (bubbleSort , TOTAL )
160
- sortTest (selectionSort , TOTAL )
161
- sortTest (insertionSort , TOTAL )
162
- sortTest (shellSort , TOTAL )
163
- sortTest (mergeSort , TOTAL )
164
- sortTest (quickSort , TOTAL )
165
- sortTest (heapSort , TOTAL )
179
+
180
+ if __name__ == '__main__' :
181
+ sortTest (bubbleSort , TOTAL )
182
+ sortTest (selectionSort , TOTAL )
183
+ sortTest (insertionSort , TOTAL )
184
+ sortTest (shellSort , TOTAL )
185
+ sortTest (mergeSort , TOTAL )
186
+ sortTest (quickSort , TOTAL )
187
+ sortTest (heapSort ,
32E0
TOTAL )
0 commit comments