Skip to content

Commit ed760f9

Browse files
committed
1 优化代码,根据PEP8格式化代码
1 parent 81491b1 commit ed760f9

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

python/12_sorts/merge_sort.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
from typing import List
66

7+
78
def merge_sort(a: List[int]):
8-
_merge_sort_between(a, 0, len(a)-1)
9+
_merge_sort_between(a, 0, len(a) - 1)
10+
911

1012
def _merge_sort_between(a: List[int], low: int, high: int):
1113
# The indices are inclusive for both low and high.
12-
if low >= high: return
13-
mid = low + (high - low)//2
14-
_merge_sort_between(a, low, mid)
15-
_merge_sort_between(a, mid+1, high)
14+
if low < high:
15+
mid = low + (high - low) // 2
16+
_merge_sort_between(a, low, mid)
17+
_merge_sort_between(a, mid + 1, high)
18+
_merge(a, low, mid, high)
1619

17-
_merge(a, low, mid, high)
1820

1921
def _merge(a: List[int], low: int, mid: int, high: int):
2022
# a[low:mid], a[mid+1, high] are sorted.
21-
i, j = low, mid+1
23+
i, j = low, mid + 1
2224
tmp = []
2325
while i <= mid and j <= high:
2426
if a[i] <= a[j]:
@@ -29,8 +31,23 @@ def _merge(a: List[int], low: int, mid: int, high: int):
2931
j += 1
3032
start = i if i <= mid else j
3133
end = mid if i <= mid else high
32-
tmp.extend(a[start:end+1])
33-
a[low:high+1] = tmp
34+
tmp.extend(a[start:end + 1])
35+
a[low:high + 1] = tmp
36+
37+
38+
def test_merge_sort():
39+
a1 = [3, 5, 6, 7, 8]
40+
merge_sort(a1)
41+
assert a1 == [3, 5, 6, 7, 8]
42+
a2 = [2, 2, 2, 2]
43+
merge_sort(a2)
44+
assert a2 == [2, 2, 2, 2]
45+
a3 = [4, 3, 2, 1]
46+
merge_sort(a3)
47+
assert a3 == [1, 2, 3, 4]
48+
a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
49+
merge_sort(a4)
50+
assert a4 == [-2, -1, 3, 3, 5, 7, 8, 9, 9]
3451

3552

3653
if __name__ == "__main__":
@@ -45,4 +62,4 @@ def _merge(a: List[int], low: int, mid: int, high: int):
4562
merge_sort(a3)
4663
print(a3)
4764
merge_sort(a4)
48-
print(a4)
65+
print(a4)

python/12_sorts/quick_sort.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,47 @@
55
from typing import List
66
import random
77

8+
89
def quick_sort(a: List[int]):
9-
_quick_sort_between(a, 0, len(a)-1)
10+
_quick_sort_between(a, 0, len(a) - 1)
11+
1012

1113
def _quick_sort_between(a: List[int], low: int, high: int):
12-
if low >= high: return
13-
# get a random position as the pivot
14-
k = random.randint(low, high)
15-
a[low], a[k] = a[k], a[low]
14+
if low < high:
15+
# get a random position as the pivot
16+
k = random.randint(low, high)
17+
a[low], a[k] = a[k], a[low]
18+
19+
m = _partition(a, low, high) # a[m] is in final position
20+
_quick_sort_between(a, low, m - 1)
21+
_quick_sort_between(a, m + 1, high)
1622

17-
m = _partition(a, low, high) # a[m] is in final position
18-
_quick_sort_between(a, low, m-1)
19-
_quick_sort_between(a, m+1, high)
2023

2124
def _partition(a: List[int], low: int, high: int):
2225
pivot, j = a[low], low
23-
for i in range(low+1, high+1):
26+
for i in range(low + 1, high + 1):
2427
if a[i] <= pivot:
2528
j += 1
2629
a[j], a[i] = a[i], a[j] # swap
2730
a[low], a[j] = a[j], a[low]
2831
return j
2932

3033

34+
def test_quick_sort():
35+
a1 = [3, 5, 6, 7, 8]
36+
quick_sort(a1)
37+
assert a1 == [3, 5, 6, 7, 8]
38+
a2 = [2, 2, 2, 2]
39+
quick_sort(a2)
40+
assert a2 == [2, 2, 2, 2]
41+
a3 = [4, 3, 2, 1]
42+
quick_sort(a3)
43+
assert a3 == [1, 2, 3, 4]
44+
a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
45+
quick_sort(a4)
46+
assert a4 == [-2, -1, 3, 3, 5, 7, 8, 9, 9]
47+
48+
3149
if __name__ == "__main__":
3250
a1 = [3, 5, 6, 7, 8]
3351
a2 = [2, 2, 2, 2]
@@ -40,4 +58,4 @@ def _partition(a: List[int], low: int, high: int):
4058
quick_sort(a3)
4159
print(a3)
4260
quick_sort(a4)
43-
print(a4)
61+
print(a4)

0 commit comments

Comments
 (0)