-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBubble.java
45 lines (44 loc) · 984 Bytes
/
Bubble.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
class Bubble
{
public static void main(String ar[])
{
int n,c,d,swap;
Scanner in=new Scanner(System.in);
Random r=new Random();
System.out.println("Input no.of integers to sort");
n=in.nextInt();
int array[]=new int[n];
System.out.println("Unsorted Array:");
for(int i=0;i<n;i++){
array[i]=r.nextInt(n)+1;
//System.out.println(array[i]+" ");
}
long startTime = System.nanoTime();
for(c=0;c<(n-1);c++)
{
int flag=0;
for(d=0;d<n-c-1;d++)
{
if(array[d]>array[d+1])
{
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;
flag+=1;
}
}
if(flag==0)
{
break;
}
}
long endTime = System.nanoTime();
long totalTime = (endTime - startTime)/1000;
System.out.println("Sorted list of numbers");
for(c=0;c<n;c++){
//System.out.println(array[c]);
}
System.out.println("Total Execution Time "+totalTime);
}
}