next up previous
Next: Up: Previous:

Algorithm: Insertion Sort

In-Place Sort: uses only a fixed amount of storage beyond that needed for the data.

Pseudocode:

Insertion-Sort(A) $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; A is an array of numbers
1
$\;\;\;\;\;$for j = 2 to length(A)
2
$\;\;\;\;\;$ $\;\;\;\;\;$key = A[j]
3
$\;\;\;\;\;$ $\;\;\;\;\;$i = j - 1
4
$\;\;\;\;\;$ $\;\;\;\;\;$while i > 0 and A[i] > key
5
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$A[i+1] = A[i]
6
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$i = i - 1
7
$\;\;\;\;\;$ $\;\;\;\;\;$A[i+1] = key



InsertionSort



Sort2



Sort3



Compare sorting algorithms


next up previous
Next: Up: Previous: