-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort
47 lines (37 loc) · 1.03 KB
/
BubbleSort
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
46
47
/* IMPORTANT: class must not be public. */
/*
* uncomment this if you want to read input.
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
class TestClass {
public static void main(String args[] ) throws Exception {
int a[]={100,2,4,66,1,6,8,5,3,7};
System.out.println("Starting bubble sort");
// new TestClass().mergeSort(a);
// new TestClass().displayArray(a);
new TestClass().bubbleSort(a);
new TestClass().displayArray(a);
}
public void bubbleSort(int a[]){
for (int i=0;i<a.length ;i++ ){
int flag=0;
for (int j=0;j<a.length-i-1;j++)
{
if (a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if (flag==0) break;
}
}
public void displayArray(int x[]){
for(int i:x){
System.out.print(i+" ");
}
System.out.println(" ");
}
}