manthanthakker40

InsertionSort

Jan 2nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package insertionsort;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author manth
  13.  */
  14. public class InsertionSort {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     static int n;
  20.     static double input[];
  21.     static String ALGO="InsertionSort";
  22.     public static void main(String[] args) {
  23.  
  24.         //input();
  25.        
  26.         randinput();
  27.         System.out.println(" INPUT IS ");
  28.        
  29.         display(input);
  30.         input=insertionsort(input);
  31.          System.out.println("OUTPUT OF SORTED NUMBERS IS ");
  32.          System.out.println("Numbers are sorted by "+ ALGO);
  33.         display(input);
  34.  
  35.      
  36.     }
  37.    
  38.     public static double[] insertionsort(double input[])
  39.     {
  40.        
  41.         long startTime = System.currentTimeMillis();
  42.         int count=0;
  43.         for(int i=1;i<input.length;i++)
  44.         {
  45.            
  46.             for(int j=i;j>0;j--)
  47.             {
  48.                 if(input[j]<input[j-1])
  49.                 {
  50.                     double temp=input[j];
  51.                     input[j]=input[j-1];
  52.                     input[j-1]=temp;
  53.                     count++;
  54.                 }
  55.                 else
  56.                     break;
  57.             }
  58.         }
  59.         System.out.println(count +" passes");
  60.        
  61.        
  62.        
  63.         long endTime=System.currentTimeMillis();
  64.         System.out.println("\n\n time elapsed in sorting is "+ (endTime-startTime)+" Milliseconds \n\n");
  65.         return input;
  66.     }
  67.  
  68.     public static void display(double [] input)
  69.     {
  70.        
  71.         for(int i=0;i<input.length;i++)
  72.         {
  73.             System.out.println(input[i]+" ");
  74.         }
  75.     }
  76.     public static void randinput()
  77.     {
  78.         Scanner sc=new Scanner(System.in);
  79.         System.out.println(" BUBBLE SORT \n ENTER TOTAL NUMBERS TO SORT");
  80.         try{
  81.         n=Integer.parseInt(sc.nextLine());
  82.         }catch(Exception e)
  83.         {
  84.             System.out.println(" Please Enter a valid integer "+e);
  85.         }  
  86.         input=new double[n];
  87.         for(int i=0;i<n;i++)
  88.         {
  89.             input[i]=Math.random()*1000;
  90.         }
  91.     }
  92.     public static void input()
  93.     {
  94.         Scanner sc=new Scanner(System.in);
  95.         System.out.println(" \n ENTER TOTAL NUMBERS TO SORT");
  96.         try{
  97.         n=Integer.parseInt(sc.nextLine());
  98.         }catch(Exception e)
  99.         {
  100.             System.out.println(" Please Enter a valid integer "+e);
  101.         }  
  102.         input=new double[n];
  103.  
  104.         for(int i=0;i<n;i++)
  105.         {
  106.             input[i]=sc.nextInt();
  107.         }
  108.         System.out.println(" INPUT RECORDED ");
  109.         System.out.println(" Numbers are getting sorted by algorithm: "+ ALGO);
  110.  
  111.     }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment