forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble.java
More file actions
38 lines (35 loc) · 996 Bytes
/
Bubble.java
File metadata and controls
38 lines (35 loc) · 996 Bytes
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
import java.util.*;
class Bubble {
public static void main(String[] args) {
int a[]= new int[1000];
for(int i=0;i<1000;i++){
a[i]=i;
}
long s= System.nanoTime();
int swap =0;
int comp=0;
for (int i=0;i<999;i++)
{
for (int j=0;j<999;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap++;
}
comp++;
}
}
System.out.println("sorted array is:");
long e = System.nanoTime();
for(int i=0;i<1000;i++)
{
System.out.print(a[i]+",");
}
System.out.println("\ntotal time in nano secoend:"+(e-s));
System.out.println("no of swap: "+swap);
System.out.println("no of comparisons: "+comp);
}
}