1 解决代码冲突
2 优化代码,根据PEP8格式化代码 3 增加pytest单元测试 4 减少打印输出
This commit is contained in:
parent
17bbd628fa
commit
81491b122b
@ -7,28 +7,30 @@
|
|||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
# 冒泡排序
|
# 冒泡排序
|
||||||
def bubble_sort(a: List[int]):
|
def bubble_sort(a: List[int]):
|
||||||
if len(a) <= 1: return
|
length = len(a)
|
||||||
|
if length <= 1:
|
||||||
|
return
|
||||||
|
|
||||||
<<<<<<< HEAD
|
for i in range(length):
|
||||||
made_swap = False
|
made_swap = False
|
||||||
for i in range(len(a)):
|
for j in range(length - i - 1):
|
||||||
=======
|
|
||||||
for i in range(len(a)):
|
|
||||||
made_swap = False
|
|
||||||
>>>>>>> upstream/master
|
|
||||||
for j in range(len(a) - i - 1):
|
|
||||||
if a[j] > a[j + 1]:
|
if a[j] > a[j + 1]:
|
||||||
a[j], a[j + 1] = a[j + 1], a[j]
|
a[j], a[j + 1] = a[j + 1], a[j]
|
||||||
made_swap = True
|
made_swap = True
|
||||||
if not made_swap: break
|
if not made_swap:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
# 插入排序
|
# 插入排序
|
||||||
def insertion_sort(a: List[int]):
|
def insertion_sort(a: List[int]):
|
||||||
if len(a) <= 1: return
|
length = len(a)
|
||||||
|
if length <= 1:
|
||||||
|
return
|
||||||
|
|
||||||
for i in range(1, len(a)):
|
for i in range(1, length):
|
||||||
value = a[i]
|
value = a[i]
|
||||||
j = i - 1
|
j = i - 1
|
||||||
while j >= 0 and a[j] > value:
|
while j >= 0 and a[j] > value:
|
||||||
@ -36,65 +38,68 @@ def insertion_sort(a: List[int]):
|
|||||||
j -= 1
|
j -= 1
|
||||||
a[j + 1] = value
|
a[j + 1] = value
|
||||||
|
|
||||||
|
|
||||||
# 选择排序
|
# 选择排序
|
||||||
def selection_sort(a: List[int]):
|
def selection_sort(a: List[int]):
|
||||||
if len(a) <= 1: return
|
length = len(a)
|
||||||
|
if length <= 1:
|
||||||
|
return
|
||||||
|
|
||||||
for i in range(len(a)):
|
for i in range(length):
|
||||||
min_index = i
|
min_index = i
|
||||||
min_val = a[i]
|
min_val = a[i]
|
||||||
for j in range(i, len(a)):
|
for j in range(i, length):
|
||||||
if a[j] < min_val:
|
if a[j] < min_val:
|
||||||
min_val = a[j]
|
min_val = a[j]
|
||||||
min_index = j
|
min_index = j
|
||||||
a[i], a[min_index] = a[min_index], a[i]
|
a[i], a[min_index] = a[min_index], a[i]
|
||||||
|
|
||||||
|
|
||||||
|
def test_bubble_sort():
|
||||||
|
test_array = [1, 1, 1, 1]
|
||||||
|
bubble_sort(test_array)
|
||||||
|
assert test_array == [1, 1, 1, 1]
|
||||||
|
test_array = [4, 1, 2, 3]
|
||||||
|
bubble_sort(test_array)
|
||||||
|
assert test_array == [1, 2, 3, 4]
|
||||||
|
test_array = [4, 3, 2, 1]
|
||||||
|
bubble_sort(test_array)
|
||||||
|
assert test_array == [1, 2, 3, 4]
|
||||||
|
|
||||||
|
|
||||||
|
def test_insertion_sort():
|
||||||
|
test_array = [1, 1, 1, 1]
|
||||||
|
insertion_sort(test_array)
|
||||||
|
assert test_array == [1, 1, 1, 1]
|
||||||
|
test_array = [4, 1, 2, 3]
|
||||||
|
insertion_sort(test_array)
|
||||||
|
assert test_array == [1, 2, 3, 4]
|
||||||
|
test_array = [4, 3, 2, 1]
|
||||||
|
insertion_sort(test_array)
|
||||||
|
assert test_array == [1, 2, 3, 4]
|
||||||
|
|
||||||
|
|
||||||
|
def test_selection_sort():
|
||||||
|
test_array = [1, 1, 1, 1]
|
||||||
|
selection_sort(test_array)
|
||||||
|
assert test_array == [1, 1, 1, 1]
|
||||||
|
test_array = [4, 1, 2, 3]
|
||||||
|
selection_sort(test_array)
|
||||||
|
assert test_array == [1, 2, 3, 4]
|
||||||
|
test_array = [4, 3, 2, 1]
|
||||||
|
selection_sort(test_array)
|
||||||
|
assert test_array == [1, 2, 3, 4]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
array = [1, 1, 1, 1]
|
|
||||||
bubble_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [1, 2, 3, 4]
|
|
||||||
bubble_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [4, 3, 2, 1]
|
|
||||||
bubble_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
|
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
|
||||||
bubble_sort(array)
|
bubble_sort(array)
|
||||||
print(array)
|
print(array)
|
||||||
|
|
||||||
array = [1, 1, 1, 1]
|
|
||||||
insertion_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [1, 2, 3, 4]
|
|
||||||
insertion_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [4, 3, 2, 1]
|
|
||||||
insertion_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
|
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
|
||||||
insertion_sort(array)
|
insertion_sort(array)
|
||||||
print(array)
|
print(array)
|
||||||
|
|
||||||
array = [1, 1, 1, 1]
|
|
||||||
selection_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [1, 2, 3, 4]
|
|
||||||
selection_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [4, 3, 2, 1]
|
|
||||||
selection_sort(array)
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
|
array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
|
||||||
selection_sort(array)
|
selection_sort(array)
|
||||||
print(array)
|
print(array)
|
Loading…
Reference in New Issue
Block a user