arr = [2, 4, 1, 5, 0, 9, 3, 7]
n = len(arr)
for i in range(0, n):
bubble = False
for j in range(0, n - 1 - i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
bubble = True
if not bubble:
break
print(arr)
arr = [8, 4, 0, 3, 1, 5, 2, 7]
n = len(arr)
for i in range(0, n):
min_index = i
for j in range(i+1, n):
if arr[j] < arr[min_index]:
min_index = j
if min_index != i:
arr[i], arr[min_index] = arr[min_index], arr[i]
print(arr)
arr = [3, 1, 4, 0, 5, 9, 7, 6, 3, 8]
n = len(arr)
for i in range(1, n):
pos = 0
for j in range(0, i):
if arr[i] > arr[j]:
pos = j+1
arr.insert(pos, arr.pop(i))
print(arr)
arr = [3, 1, 4, 0, 5, 9, 7, 6, 3, 8]
n = len(arr)
for i in range(1, n):
j = i -1
cur = arr[i]
while j >= 0 and arr[j] > cur:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = cur
print(arr)
arr = [3, 1, 4, 0, 5, 9, 7, 6, 3, 8]
n = len(arr)
for i in range(1, n):
cur = arr[i]
pos = i
for j in range(i - 1, -1, -1):
if arr[j] > cur:
arr[j+1] = arr[j]
pos = j
arr[pos] = cur
print(arr)
arr = [5, 8, 10, 2, 7, 9, 15, 78, 34, 90, 21, 54, 37, 39, 48, 26, 87, 72, 3, 6, 15, 12, 64, 62, 44, 7, 88]
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
j = i - gap
cur = arr[i]
while j >= 0 and arr[j+gap] < arr[j]:
arr[j+gap] = arr[j]
j-=gap
arr[j+gap] = cur
gap //= 2
print(arr)
十大经典排序算法动画与解析,看我就够了!(配代码完全版)
LeetCodeAnimation