One of the simplest methods to sort an array is an insertion sort. An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence. Both average and worst-case time is O(n2).

Assuming there are n elements in the array, we must index through n – 1 entries. For each entry, we may need to examine and shift up to n – 1 other entries, resulting in a O(n2) algorithm. The insertion sort is an in-place sort. That is, we sort the array in-place. No extra memory is required. The insertion sort is also a stable sort

Advantage: Relative simple and easy to implement. Twice faster than bubble sort.

 Disadvantage: Inefficient for large lists.

Implementation in Java

public  int[] insertionSort(int arr[])                             //

{

int n= arr.length;

for(int i=1;i<n;i++)

{

int key = arr[i];

int j=i;

while((j>0)&&(arr[j-1]>key))

{

arr[j]=arr[j-1];

j–;

}

arr[j]=key;

}

return arr;

}