Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package selectionsort;
- import java.util.Scanner;
- /**
- *
- * @author manth
- */
- public class SelectionSort {
- /**
- * @param args the command line arguments
- */
- static int n;
- static double input[];
- static String ALGO="SelectionSort";
- public static void main(String[] args) {
- //input();
- randinput();
- input=selectionsort(input);
- display(input);
- }
- public static double[] selectionsort(double input[])
- {
- long startTime = System.currentTimeMillis();
- for(int i=0;i<input.length;i++)
- {
- int min=i;
- for(int j=i+1;j<input.length;j++)
- {
- if(input[min]>input[j])
- min=j;
- }
- double temp=input[i];
- input[i]=input[min];
- input[min]=temp;
- }
- Long endTime=System.currentTimeMillis();
- System.out.println("time elapsed in sorting is "+ (endTime-startTime)+" Milliseconds ");
- return input;
- }
- public static void display(double [] input)
- {
- System.out.println(" Numbers are sorted by "+ ALGO);
- for(int i=0;i<input.length;i++)
- {
- System.out.println(input[i]+" ");
- }
- }
- public static void randinput()
- {
- Scanner sc=new Scanner(System.in);
- System.out.println(" BUBBLE SORT \n ENTER TOTAL NUMBERS TO SORT");
- try{
- n=Integer.parseInt(sc.nextLine());
- }catch(Exception e)
- {
- System.out.println(" Please Enter a valid integer "+e);
- }
- input=new double[n];
- for(int i=0;i<n;i++)
- {
- input[i]=Math.random()*1000;
- }
- }
- public static void input()
- {
- Scanner sc=new Scanner(System.in);
- System.out.println(" BUBBLE SORT \n ENTER TOTAL NUMBERS TO SORT");
- try{
- n=Integer.parseInt(sc.nextLine());
- }catch(Exception e)
- {
- System.out.println(" Please Enter a valid integer "+e);
- }
- input=new double[n];
- for(int i=0;i<n;i++)
- {
- input[i]=sc.nextInt();
- }
- System.out.println(" INPUT RECORDED ");
- System.out.println(" Numbers are getting sorted by algorithm: "+ ALGO);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement