public class Insertionsort
{
public static void main(String[] args)
{
int[] array={88,77,66,55,44};
insertionsort(array);
display(array);
}
public static void insertionsort(int[] arr)
{
for(int counter=1;counter<arr.length-1;counter++)
{
int val=arr[counter];
int j=counter-1;
while(j>=0 && arr[j]>val) // while(arr[j]>val && j<=0) doesn’t work. Why is that so?
{
arr[j+1]=arr[j];
j–;
}
arr[j+1]=val;
}
}
public static void display(int[] arr)
{
System.out.println("sorted elements are ");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}