Advertisement
manthanthakker40

SelectionSort

Jan 2nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 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 selectionsort;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author manth
  13.  */
  14. public class SelectionSort {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.       static int n;
  20.     static double input[];
  21.     static String ALGO="SelectionSort";
  22.     public static void main(String[] args) {
  23.  
  24.         //input();
  25.         randinput();
  26.        
  27.         input=selectionsort(input);
  28.        
  29.         display(input);
  30.  
  31.      
  32.     }
  33.    
  34.     public static double[] selectionsort(double input[])
  35.     {
  36.          long startTime = System.currentTimeMillis();
  37.         for(int i=0;i<input.length;i++)
  38.         {
  39.             int min=i;
  40.             for(int j=i+1;j<input.length;j++)
  41.             {
  42.                 if(input[min]>input[j])
  43.                    min=j;
  44.                    
  45.             }
  46.             double temp=input[i];
  47.             input[i]=input[min];
  48.             input[min]=temp;
  49.         }
  50.         Long endTime=System.currentTimeMillis();
  51.             System.out.println("time elapsed in sorting is "+ (endTime-startTime)+" Milliseconds ");
  52.         return input;
  53.     }
  54.     public static void display(double [] input)
  55.     {
  56.         System.out.println(" Numbers are sorted by "+ ALGO);
  57.         for(int i=0;i<input.length;i++)
  58.         {
  59.             System.out.println(input[i]+" ");
  60.         }
  61.     }
  62.     public static void randinput()
  63.     {
  64.         Scanner sc=new Scanner(System.in);
  65.         System.out.println(" BUBBLE SORT \n ENTER TOTAL NUMBERS TO SORT");
  66.         try{
  67.         n=Integer.parseInt(sc.nextLine());
  68.         }catch(Exception e)
  69.         {
  70.             System.out.println(" Please Enter a valid integer "+e);
  71.         }  
  72.         input=new double[n];
  73.         for(int i=0;i<n;i++)
  74.         {
  75.             input[i]=Math.random()*1000;
  76.         }
  77.     }
  78.     public static void input()
  79.     {
  80.         Scanner sc=new Scanner(System.in);
  81.         System.out.println(" BUBBLE SORT \n ENTER TOTAL NUMBERS TO SORT");
  82.         try{
  83.         n=Integer.parseInt(sc.nextLine());
  84.         }catch(Exception e)
  85.         {
  86.             System.out.println(" Please Enter a valid integer "+e);
  87.         }  
  88.         input=new double[n];
  89.  
  90.         for(int i=0;i<n;i++)
  91.         {
  92.             input[i]=sc.nextInt();
  93.         }
  94.         System.out.println(" INPUT RECORDED ");
  95.         System.out.println(" Numbers are getting sorted by algorithm: "+ ALGO);
  96.  
  97.     }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement