2020-03-02
Insertion sort(A,n)
for j = 1 to n {
key = A[j]
i = j-1
while( i >= 0 && A[i] > key) {
A[i+1] = A[i]
i--
}
A[i+1] = key
}
kroki:
[X][ ][ ]...[ ]
1 2 3 ... n
m
[X][X][ ]...[a_k]|...[ ]
1 2 3 ...k ... n
Liczba porównań pomiędzy elementami input’u
[9, 7, 5, 3, 1]
1 2 3 4 5
[5, 7, 9, 3, 1]
przy czym dlatego