插入排序:
1 2 3 4 5 6 7 8 9 |
a = list(map(int, input().split())) n = len(a) for i in range(1, n): while (i - 1 >= 0 and a[i] < a[i - 1]): a[i], a[i - 1] = a[i - 1], a[i] i -= 1 print(a) |
选择排序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
a = list(map(int, input().split())) n = len(a) Compare, Swap = 0, 0 for i in range(n - 1): k = i for j in range(i + 1, n): if (a[j] <= a[k]): k = j Compare += 1 if (k != i): a[k], a[i] = a[i], a[k] Swap += 1 print(a) print(Compare, Swap) |
发表回复