-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorts.py
50 lines (42 loc) · 1.38 KB
/
sorts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def insert_sort(A):
""" сортировка списка А вставками"""
N = len(A)
for top in range(1, N):
k = top
while k > 0 and A[k - 1] > A[k]:
A[k], A[k - 1] = A[k - 1], A[k]
k -= 1
def choise_sort(A):
""" сортировка списка А выбором"""
N = len(A)
for pos in range(0, N - 1):
for k in range(pos + 1, N):
if A[k] < A[pos]:
A[k], A[pos] = A[pos], A[k]
def bubble_sort(A):
""" сортировка списка А методом пузырька"""
N = len(A)
for bypass in range(1, N):
for k in range(0, N - bypass):
if A[k] > A[k + 1]:
A[k], A[k + 1] = A[k + 1], A[k]
def test_sort(sort_algoritm):
print("тестируем:", sort_algoritm.__doc__)
print("testcase #1: ", end="")
A = [4, 2, 5, 1, 3]
A_sorted = [1, 2, 3, 4, 5]
sort_algoritm(A)
print("ok" if A == A_sorted else "failed")
print("testcase #2: ", end="")
A = list(range(10, 20)) + list(range(0, 10))
A_sorted = list(range(20))
sort_algoritm(A)
print("ok" if A == A_sorted else "failed")
print("testcase #3: ", end="")
A = [4, 2, 4, 2, 1]
A_sorted = [1, 2, 2, 4, 4]
sort_algoritm(A)
print("ok" if A == A_sorted else "failed")
test_sort(insert_sort)
test_sort(choise_sort)
test_sort(bubble_sort)