Implementatie 1: Insertion sort
i ← 1
while i < length(A)
x ← A[i]
j ← i - 1
while j >= 0 and A[j] > x
A[j+1] ← A[j]
j ← j - 1
end while
A[j+1] ← x[4]
i ← i + 1
end while
Implementatie 2: Recursieve insertion sort
function insertionSortR(array A, int n)
if n>0
insertionSortR(A,n-1)
x ← A[n]
j ← n-1
while j >= 0 and A[j] > x
A[j+1] ← A[j]
j ← j-1
end while
A[j+1] ← x
end if
end function
De eerste implementatie maakt gebruik van twee loops waar de tweede implementatie gebruik maakt van slechts een loop en het roept zichzelf aan (recursief).
Bron: https://en.wikipedia.org/wiki/Insertion_sort
Best case is een al gesorteerde lijst want dan moet hij alleen voor elk element kijken of hij op de goede plek staat. O(n)
Worst case is als de lijst precies verkeerd om staat. O(n^2)
Voorbeeld (als ik het goed begrijp): Lijst heeft 10 elementen. Best case: 10 Worst case: 100