next up previous
Next: Analyzing Algorithms Up: CSE 2320: Algorithms and Previous: Example: Sorting

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



Compare sorting algorithms



Copyright © 1998 The University of Texas at Arlington