Insertion Sort

Insert each element into its sorted position.

Insertion sort: Build sorted array by inserting each element into correct position.

Left portion sorted. Take next unsorted element. Shift larger elements right. Insert.

Example [5,2,4,6,1,3][5,2,4,6,1,3]: 1.1. [5][5] sorted. Insert 22: [2,5][2,5]

2.2. Insert 44: [2,4,5][2,4,5]

3.3. Insert 66: [2,4,5,6][2,4,5,6]

4.4. Insert 11: [1,2,4,5,6][1,2,4,5,6]

5.5. Insert 33: [1,2,3,4,5,6][1,2,3,4,5,6]

Time: O(n2)O(n^2) worst, O(n)O(n) nearly sorted.