public class demo1
{
public static void insertionSort(int array[])
{
int arrSize = array.length;
for (int b = 1; b< arrSize; b++)
{
int key = array[b];
int a = b-1;
while ( (a > -1) && ( array [a] > key ) )
{
array [a+1] = array [a];
a--;
}
array[a+1] = key;
}
}
public static void main(String arr[])
{
int[] array1 = {45,32,43,2,4,4,53};
System.out.println("Before Insertion Sort");
for(int a:array1)
{
System.out.print(a+" ");
}
System.out.println();
insertionSort(array1);
System.out.println("After Insertion Sort");
for(int a:array1){
System.out.print(a+" ");
}
}
}Java
Program to implement Insertion Sorting.

Written by
View ProfileNikhil Juneja
Passionate software developer and technical contributor at TutorialsLink.
