Skip to content

Commit 3b34cfc

Browse files
authored
Merge pull request hustcc#2 from LokiSharp/patch-1
Patch 1
2 parents ae6fad9 + c5d058a commit 3b34cfc

File tree

8 files changed

+173
-8
lines changed

8 files changed

+173
-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+
```

0 commit comments

Comments
 (0)