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 bubblesort;
- import java.util.Scanner;
- /**
- *
- * @author manth
- */
- public class BubbleSort {
- /**
- * @param args the command line arguments
- */
- static int n;
- static double input[];
- static String ALGO="BubbleSort";
- public static void main(String[] args) {
- //input();
- randinput();
- input=bubblesort(input);
- display(input);
- }
- public static double[] bubblesort(double []input)
- {
- long startTime = System.currentTimeMillis();
- for(int i=input.length-1;i>0;i--)
- {
- for(int j=0;j<i;j++)
- {
- if(input[j+1]<input[j])
- {
- double temp=input[j+1];
- input[j+1]=input[j];
- input[j]=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