Social Icons

Monday 16 September 2013

insertion sort in java


import java.util.Scanner;
class insert
{
  public static void main(String args[])
  {
  Scanner s = new Scanner(System.in);
  int i,b,c,temp;
  int j;
  int a[] = new int[5];
  System.out.println("\n Enter Element into the array: ");
  for(i=0;i<5;i++)
  {
   a[i]=s.nextInt();
  }
  System.out.println("\n The Element into the array are : ");
  for(i=0;i<5;i++)
  {
   System.out.print(a[i]+" ");
  }




   for(i=0;i<5;i++)
   {
    temp=a[i];
j=i-1;
   while(j>=0 && temp<a[j])
    {
      a[j+1]=a[j];

 j=j-1;
}
 a[j+1]=temp;

 
  }

    System.out.println("\n The Element after sorting into the array are : ");
  for(i=0;i<5;i++)
  {
   System.out.print(a[i]+" ");
  }





 }

}

No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management