INSERTION SORT In Java
JAVA Code :)
public class insertion
{
public static void main(String[] args)
{
int i,n,j, temp;
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of Array:");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter "+n+" elements in Array:");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
sc.close();
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
System.out.print("Elements in Array after insertion sort.");
for(i=0;i<n;i++)
{
System.out.print("\n"+a[i]);
}
}
}
0 Comments