-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
43 lines (35 loc) · 952 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
43 lines (35 loc) · 952 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
39
40
41
42
43
import java.util.Arrays;;
public class BubbleSort{
public String bubbleSort(int a[], int n){
boolean swapped;
int temp;
for(int i = 0; i < n; i++){
swapped = false;
for(int j = 0; j < n - i - 1; j++){
if(a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
swapped = true;
}
}
if(swapped == false)
break;
}
return "sorted Array a[]: " + Arrays.toString(a);
}
public static void main(String[] args){
int a[] = new int[3];
// for(int i = 0; i < 4; i++){
// a[i] = (int)(Math.random() * 10);
// }
a[0] = 0;
a[1] = 2;
a[2] = 1;
a[3] = 2;
a[4] = 0;
System.out.println("Original array: a[] : " + Arrays.toString(a));
BubbleSort sorted = new BubbleSort();
System.out.println(sorted.bubbleSort(a, a.length));
}
}