manthanthakker40

Bubble Sort

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