Algorithm


Insertion Sort Algorithm:

  1. Start with an unsorted array.
  2. Consider the first element as "sorted".
  3. For each remaining element in the array:
    1. Extract the current element and compare it with elements in the "sorted" portion of the array.
    2. Shift all larger elements one position to the right.
    3. Insert the current element into its correct position in the "sorted" portion.
  4. Repeat the process until all elements are sorted.

Example:

Insertion Sort Example

Fig. 1: Iteration by Iteration Visualization of Insertion Sort

Note: Insertion Sort ensures elements are sorted by repeatedly inserting each new element into its correct position.