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 17fd264

Browse files
LokiSharphustcc
authored andcommitted
添加算法的 Python 实现(1~5) (hustcc#2)
* Add Python * Add Python * Update 3.insertionSort.md * Add Python * Add Python * Fix * Fix * Update 3.insertionSort.md * Add Python * Add Python * Fix * Add Python * Create pythonSortTest.py * Update 3.insertionSort.md
1 parent ae6fad9 commit 17fd264

File tree

9 files changed

+338
-8
lines changed

9 files changed

+338
-8
lines changed

‎1.bubbleSort.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ function bubbleSort(arr) {
4747
}
4848
return arr;
4949
}
50-
```
50+
```
51+
52+
53+
## 5. Python 代码实现
54+
55+
```python
56+
def bubbleSort(arr):
57+
for i in range(1, len(arr)):
58+
for j in range(0, len(arr)-i):
59+
if arr[j] > arr[j+1]:
60+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
61+
return arr
62+
```

‎2.selectionSort.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,15 @@ function selectionSort(arr) {
3636
}
3737
return arr;
3838
}
39-
```
39+
```
40+
41+
## 4. Python 代码实现
42+
43+
```python
44+
def selectionSort(arr):
45+
for i in range(len(arr)-1):
46+
for j in range(i+1, len(arr)):
47+
if arr[j] < arr[i]:
48+
arr[i], arr[j] = arr[j], arr[i]
49+
return arr
50+
```

‎3.insertionSort.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,18 @@ function insertionSort(arr) {
3434
}
3535
return arr;
3636
}
37-
```
37+
```
38+
39+
## 4. Python 代码实现
40+
41+
```python
42+
def insertionSort(arr):
43+
for i in range(len(arr)):
44+
preIndex = i-1
45+
current = arr[i]
46+
while preIndex >= 0 and arr[preIndex] > current:
47+
arr[preIndex+1] = arr[preIndex]
48+
preIndex-=1
49+
arr[preIndex+1] = current
50+
return arr
51+
```

‎4.shellSort.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,25 @@ function shellSort(arr) {
4040
}
4141
return arr;
4242
}
43-
```
43+
```
44+
45+
## 3. Python 代码实现
46+
47+
```python
48+
def shellSort(arr):
49+
import math
50+
gap=1
51+
while(gap < len(arr)/3):
52+
gap = gap*3+1
53+
while gap > 0:
54+
for i in range(gap,len(arr)):
55+
temp = arr[i]
56+
j = i-gap
57+
while j >=0 and arr[j] > temp:
58+
arr[j+gap]=arr[j]
59+
j-=gap
60+
arr[j+gap] = temp
61+
gap = math.floor(gap/3)
62+
return arr
63+
}
64+
```

‎5.mergeSort.md‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,29 @@ function merge(left, right)
7070

7171
return result;
7272
}
73-
```
73+
```
74+
75+
## 5. Python 代码实现
76+
77+
```python
78+
def mergeSort(arr):
79+
import math
80+
if(len(arr)<2):
81+
return arr
82+
middle = math.floor(len(arr)/2)
83+
left, right = arr[0:middle], arr[middle:]
84+
return merge(mergeSort(left), mergeSort(right))
85+
86+
def merge(left,right):
87+
result = []
88+
while left and right:
89+
if left[0] <= right[0]:
90+
result.append(left.pop(0));
91+
else:
92+
result.append(right.pop(0));
93+
while left:
94+
result.append(left.pop(0));
95+
while right:
96+
result.append(right.pop(0));
97+
return result
98+
```

‎6.quickSort.md‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,33 @@ function swap(arr, i, j) {
6262
arr[i] = arr[j];
6363
arr[j] = temp;
6464
}
65-
```
65+
```
66+
67+
68+
## 4. Python 代码实现
69+
70+
```python
71+
def quickSort(arr, left=None, right=None):
72+
left = 0 if not isinstance(left,(int, float)) else left
73+
right = len(arr)-1 if not isinstance(right,(int, float)) else right
74+
if left < right:
75+
partitionIndex = partition(arr, left, right)
76+
quickSort(arr, left, partitionIndex-1)
77+
quickSort(arr, partitionIndex+1, right)
78+
return arr
79+
80+
def partition(arr, left, right):
81+
pivot = left
82+
index = pivot+1
83+
i = index
84+
while i <= right:
85+
if arr[i] < arr[pivot]:
86+
swap(arr, i, index)
87+
index+=1
88+
i+=1
89+
swap(arr,pivot,index-1)
90+
return index-1
91+
92+
def swap(arr, i, j):
93+
arr[i], arr[j] = arr[j], arr[i]
94+
```

‎7.heapSort.md‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,38 @@ function heapSort(arr) {
7171
}
7272
return arr;
7373
}
74-
```
74+
```
75+
## 4. Python 代码实现
76+
77+
```python
78+
def buildMaxHeap(arr):
79+
import math
80+
for i in range(math.floor(len(arr)/2),-1,-1):
81+
heapify(arr,i)
82+
83+
def heapify(arr, i):
84+
left = 2*i+1
85+
right = 2*i+2
86+
largest = i
87+
if left < arrLen and arr[left] > arr[largest]:
88+
largest = left
89+
if right < arrLen and arr[right] > arr[largest]:
90+
largest = right
91+
92+
if largest != i:
93+
swap(arr, i, largest)
94+
heapify(arr, largest)
95+
96+
def swap(arr, i, j):
97+
arr[i], arr[j] = arr[j], arr[i]
98+
99+
def heapSort(arr):
100+
global arrLen
101+
arrLen = len(arr)
102+
buildMaxHeap(arr)
103+
for i in range(len(arr)-1,0,-1):
104+
swap(arr,0,i)
105+
arrLen -=1
106+
heapify(arr, 0)
107+
return arr
108+
```

‎8.countingSort.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,23 @@ function countingSort(arr, maxValue) {
3232

3333
return arr;
3434
}
35-
```
35+
```
36+
## 3. JavaScript 代码实现
37+
38+
```python
39+
def countingSort(arr, maxValue):
40+
bucketLen = maxValue+1
41+
bucket = [0]*bucketLen
42+
sortedIndex =0
43+
arrLen = len(arr)
44+
for i in range(arrLen):
45+
if not bucket[arr[i]]:
46+
bucket[arr[i]]=0
47+
bucket[arr[i]]+=1
48+
for j in range(bucketLen):
49+
while bucket[j]>0:
50+
arr[sortedIndex] = j
51+
sortedIndex+=1
52+
bucket[j]-=1
53+
return arr
54+
```

‎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 によって変換されたページ (->オリジナル) /