Insertion Sort: Difference between revisions

Add the algorithm.
Introductory page.
Line 12: Line 12:
         j = j - 1
         j = j - 1
     A[j + 1] = key
     A[j + 1] = key
}}
}}'''Insertion sort''' is an algorithm for [[Sorting Problem]], arranging an un-ordered list to an ordered list. We assume the first element of the un-ordered list to be ordered such that:
 
* '''Sub-array:''' $[1, j-1]$ is sorted.
* '''Key: $'''[j]$ is the key we concern with.
* '''Sub-array:''' $[j+1, A.length]$ is concerned.
 
As the insertion sort operates, it is guaranteed that $[1, j-1]$ is sorted.