Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e975d8c

Browse files
Merge branch 'master' into master
2 parents 1367764 + 0662645 commit e975d8c

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

‎1.bubbleSort.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ function bubbleSort(arr) {
5050
```
5151

5252

53+
5354
## 6. Python 代码实现
5455

56+
5557
```python
5658
def bubbleSort(arr):
5759
for i in range(1, len(arr)):

‎8.countingSort.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ function countingSort(arr, maxValue) {
3333
return arr;
3434
}
3535
```
36+
3637
## 3. Python 代码实现
3738

39+
3840
```python
3941
def countingSort(arr, maxValue):
4042
bucketLen = maxValue+1

‎pythonSortTest.py‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
'''
2+
# Create by LokiSharp(loki.sharp#gmail) at 2017年1月22日
3+
'''
4+
5+
TOTAL=5000
6+
7+
def sortTest(func, total=1000):
8+
import random, copy, operator, math, time
9+
arrList = [i for i in range(-math.floor(total/2),math.ceil(total/2))]
10+
arrListR = copy.deepcopy(arrList)
11+
while operator.eq(arrList,arrListR):
12+
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))
15+
start = time.clock()
16+
arrListR = func(arrListR)
17+
end = time.clock()
18+
runtime = end-start
19+
#print("--> [Sorted List]", arrListR, "Use", func.__name__,"with Total:", len(arrList))
20+
if operator.eq(arrList, arrListR):
21+
print("[Success]", func.__name__,"with Total:", len(arrList),"in %.5fs" % runtime)
22+
return True
23+
else:
24+
print("[Fail]", func.__name__,"with Total:", len(arrList),"in %.5fs" % runtime)
25+
return False
26+
27+
def bubbleSort(arr):
28+
for i in range(1, len(arr)):
29+
for j in range(0, len(arr)-i):
30+
if arr[j] > arr[j+1]:
31+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
32+
return arr
33+
34+
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]
39+
return arr
40+
41+
def insertionSort(arr):
42+
for i in range(len(arr)):
43+
preIndex = i-1
44+
current = arr[i]
45+
while preIndex >= 0 and arr[preIndex] > current:
46+
arr[preIndex+1] = arr[preIndex]
47+
preIndex-=1
48+
arr[preIndex+1] = current
49+
return arr
50+
51+
def shellSort(arr):
52+
import math
53+
gap=1
54+
while(gap < len(arr)/3):
55+
gap = gap*3+1
56+
while gap > 0:
57+
for i in range(gap,len(arr)):
58+
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)
65+
return arr
66+
67+
def mergeSort(arr):
68+
import math
69+
if(len(arr)<2):
70+
return arr
71+
middle = math.floor(len(arr)/2)
72+
left, right = arr[0:middle], arr[middle:]
73+
return merge(mergeSort(left), mergeSort(right))
74+
75+
def merge(left,right):
76+
result = []
77+
while left and right:
78+
if left[0] <= right[0]:
79+
result.append(left.pop(0));
80+
else:
81+
result.append(right.pop(0));
82+
while left:
83+
result.append(left.pop(0));
84+
while right:
85+
result.append(right.pop(0));
86+
return result
87+
88+
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
91+
if left < right:
92+
partitionIndex = partition(arr, left, right)
93+
quickSort(arr, left, partitionIndex-1)
94+
quickSort(arr, partitionIndex+1, right)
95+
return arr
96+
97+
def partition(arr, left, right):
98+
pivot = left
99+
index = pivot+1
100+
i = index
101+
while i <= right:
102+
if arr[i] < arr[pivot]:
103+
swap(arr, i, index)
104+
index+=1
105+
i+=1
106+
swap(arr,pivot,index-1)
107+
return index-1
108+
109+
def swap(arr, i, j):
110+
arr[i], arr[j] = arr[j], arr[i]
111+
112+
def buildMaxHeap(arr):
113+
import math
114+
for i in range(math.floor(len(arr)/2),-1,-1):
115+
heapify(arr,i)
116+
117+
def heapify(arr, i):
118+
left = 2*i+1
119+
right = 2*i+2
120+
largest = i
121+
if left < arrLen and arr[left] > arr[largest]:
122+
largest = left
123+
if right < arrLen and arr[right] > arr[largest]:
124+
largest = right
125+
126+
if largest != i:
127+
swap(arr, i, largest)
128+
heapify(arr, largest)
129+
130+
def swap(arr, i, j):
131+
arr[i], arr[j] = arr[j], arr[i]
132+
133+
def heapSort(arr):
134+
global arrLen
135+
arrLen = len(arr)
136+
buildMaxHeap(arr)
137+
for i in range(len(arr)-1,0,-1):
138+
swap(arr,0,i)
139+
arrLen -=1
140+
heapify(arr, 0)
141+
return arr
142+
143+
def countingSort(arr, maxValue=None):
144+
bucketLen = maxValue+1
145+
bucket = [0]*bucketLen
146+
sortedIndex =0
147+
arrLen = len(arr)
148+
for i in range(arrLen):
149+
if not bucket[arr[i]]:
150+
bucket[arr[i]]=0
151+
bucket[arr[i]]+=1
152+
for j in range(bucketLen):
153+
while bucket[j]>0:
154+
arr[sortedIndex] = j
155+
sortedIndex+=1
156+
bucket[j]-=1
157+
return arr
158+
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)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /