k=[2,3,3,4,1,50,5]
f = 1
for i in range(1, len(k)):
m = k[i]
n = k[i-1]
f = 1
if m>=n:
continue
for j in range(i-2, -1, -1):
if m > k[j]:
del k[i]
k.insert(j+1, m)
f = 0
break
if f == 1:
y = k[i]
del k[i]
k.insert(0, y)
def sort_insertion(array: list) -> list:
for i in range(1, len(array)):
x = array[i]
j = i
while j > 0 and array[j - 1] > x:
array[j] = array[j - 1]
j -= 1
array[j] = x
return array
sort_insertion([2, 3, 3, 4, 1, 50, 5]) # => [1, 2, 3, 3, 4, 5, 50]
Первый вариант: 7600 наносекунд
Второй вариант (sort_insertion): 5200 наносекунд (т.е быстрее на 32%)