-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSorter.java
More file actions
49 lines (41 loc) · 1.13 KB
/
BubbleSorter.java
File metadata and controls
49 lines (41 loc) · 1.13 KB
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
48
49
package sort;
public class BubbleSorter {
private int[] data;
public BubbleSorter(int[] data) {
if (data == null)
throw new RuntimeException("Data cannot be null");
this.data = data;
}
public void sort() {
for (int i = 0; i < data.length - 2; i++) {
for (int j = data.length - 1; j > i ; j--) {
if (data[j] < data[j - 1]) {
int tmp = data[j];
data[j] = data[j - 1];
data[j - 1] = tmp;
}
}
}
}
public int[] getData() {
return this.data;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('{');
for (int i : data) {
sb.append(i);
sb.append(',');
}
sb.deleteCharAt(sb.length()-1);
sb.append('}');
return sb.toString();
}
public static void main(String[] args) {
int[] data = {6,4,7,9,3,0,10};
BubbleSorter sorter = new BubbleSorter(data);
sorter.sort();
System.out.println(sorter);
}
}