Java Insertion Sorting
Java Insertion Sorting
Scanner;
public class freshers
{
public static void main(String[] args)
{
int i,j,k,temp;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of elements to be sorted");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter the elements to be sorted");
for(k=0;k<n;k++)
{
int num=sc.nextInt();
a[k]=num;
}
System.out.println("the elements before sorting are:");
for(int value: a)
{
System.out.println(value+" ");
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while((j>=0)&&(a[j]>temp))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
System.out.println();
System.out.println("the elements after sorting are:");
for(int value: a)
{
System.out.println(value+" ");
}
}
}