Skip to content

Commit 6b50ac0

Browse files
Merge pull request #193 from Danielyan86/master
优化python部分11_sorts 代码,增加pytest单元测试,解决冲突错误
2 parents 9a85b1e + 81491b1 commit 6b50ac0

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

python/11_sorts/sorts.py

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,94 +7,99 @@
77

88
from typing import List
99

10+
1011
# 冒泡排序
1112
def bubble_sort(a: List[int]):
12-
if len(a) <= 1: return
13-
14-
<<<<<<< HEAD
15-
made_swap = False
16-
for i in range(len(a)):
17-
=======
18-
for i in range(len(a)):
13+
length = len(a)
14+
if length <= 1:
15+
return
16+
17+
for i in range(length):
1918
made_swap = False
20-
>>>>>>> upstream/master
21-
for j in range(len(a) - i - 1):
22-
if a[j] > a[j+1]:
23-
a[j], a[j+1] = a[j+1], a[j]
19+
for j in range(length - i - 1):
20+
if a[j] > a[j + 1]:
21+
a[j], a[j + 1] = a[j + 1], a[j]
2422
made_swap = True
25-
if not made_swap: break
23+
if not made_swap:
24+
break
25+
2626

2727
# 插入排序
2828
def insertion_sort(a: List[int]):
29-
if len(a) <= 1: return
30-
31-
for i in range(1, len(a)):
29+
length = len(a)
30+
if length <= 1:
31+
return
32+
33+
for i in range(1, length):
3234
value = a[i]
3335
j = i - 1
3436
while j >= 0 and a[j] > value:
35-
a[j+1] = a[j]
37+
a[j + 1] = a[j]
3638
j -= 1
37-
a[j+1] = value
39+
a[j + 1] = value
40+
3841

3942
# 选择排序
4043
def selection_sort(a: List[int]):
41-
if len(a) <= 1: return
42-
43-
for i in range(len(a)):
44+
length = len(a)
45+
if length <= 1:
46+
return
47+
48+
for i in range(length):
4449
min_index = i
4550
min_val = a[i]
46-
for j in range(i, len(a)):
51+
for j in range(i, length):
4752
if a[j] < min_val:
4853
min_val = a[j]
4954
min_index = j
5055
a[i], a[min_index] = a[min_index], a[i]
5156

5257

53-
if __name__ == "__main__":
54-
array = [1, 1, 1, 1]
55-
bubble_sort(array)
56-
print(array)
58+
def test_bubble_sort():
59+
test_array = [1, 1, 1, 1]
60+
bubble_sort(test_array)
61+
assert test_array == [1, 1, 1, 1]
62+
test_array = [4, 1, 2, 3]
63+
bubble_sort(test_array)
64+
assert test_array == [1, 2, 3, 4]
65+
test_array = [4, 3, 2, 1]
66+
bubble_sort(test_array)
67+
assert test_array == [1, 2, 3, 4]
68+
69+
70+
def test_insertion_sort():
71+
test_array = [1, 1, 1, 1]
72+
insertion_sort(test_array)
73+
assert test_array == [1, 1, 1, 1]
74+
test_array = [4, 1, 2, 3]
75+
insertion_sort(test_array)
76+
assert test_array == [1, 2, 3, 4]
77+
test_array = [4, 3, 2, 1]
78+
insertion_sort(test_array)
79+
assert test_array == [1, 2, 3, 4]
80+
81+
82+
def test_selection_sort():
83+
test_array = [1, 1, 1, 1]
84+
selection_sort(test_array)
85+
assert test_array == [1, 1, 1, 1]
86+
test_array = [4, 1, 2, 3]
87+
selection_sort(test_array)
88+
assert test_array == [1, 2, 3, 4]
89+
test_array = [4, 3, 2, 1]
90+
selection_sort(test_array)
91+
assert test_array == [1, 2, 3, 4]
5792

58-
array = [1, 2, 3, 4]
59-
bubble_sort(array)
60-
print(array)
61-
62-
array = [4, 3, 2, 1]
63-
bubble_sort(array)
64-
print(array)
6593

94+
if __name__ == "__main__":
6695
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
6796
bubble_sort(array)
6897
print(array)
6998

70-
array = [1, 1, 1, 1]
71-
insertion_sort(array)
72-
print(array)
73-
74-
array = [1, 2, 3, 4]
75-
insertion_sort(array)
76-
print(array)
77-
78-
array = [4, 3, 2, 1]
79-
insertion_sort(array)
80-
print(array)
81-
8299
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
83100
insertion_sort(array)
84101
print(array)
85102

86-
array = [1, 1, 1, 1]
87-
selection_sort(array)
88-
print(array)
89-
90-
array = [1, 2, 3, 4]
91-
selection_sort(array)
92-
print(array)
93-
94-
array = [4, 3, 2, 1]
95-
selection_sort(array)
96-
print(array)
97-
98103
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
99104
selection_sort(array)
100-
print(array)
105+
print(array)

0 commit comments

Comments
 (0)