How to do InsertionSort in JAVA

package sortingpackage;

 

public class InsertionSort {
public static void main (String [] args){

int[] a={3,6,8,4,-4,-55,-67,33};
for (int i=0; i<=a.length-2;i++)
{
if (a[i]<=a[i+1])
continue;
int t= a[i+1];
int j= i+1;
while(j>=1&& a[j-1]>t)
{
a [j]=a[j-1];
j–;
}
a[j]=t;
}
System.out.print(“now the final array =\n”);

for(int i=0;i<=a.length-1;i++)
System.out.print(“,” + a[i]);
System.out.println();

}
}

After Running Program:)

Leave a Reply