-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortTest.java
More file actions
52 lines (44 loc) · 1.39 KB
/
BubbleSortTest.java
File metadata and controls
52 lines (44 loc) · 1.39 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
50
51
52
// javac BubbleSortTest.java
//
// java BubbleSortTest
public class BubbleSortTest {
public static int[] bubbleSort(int[] array) {
if (array != null) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
return array;
}
public static String toString(int[] array) {
if (array != null) {
StringBuffer sb = new StringBuffer("");
sb.append("[");
for (int i = 0; i < array.length; i++) {
sb.append(Integer.toString(array[i]));
if (i < array.length - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
} else {
return null;
}
}
public static void main(String[] args) {
int[] array = { 64, 34, 25, 12, 22, 11, 90 };
System.out.println("Original array:");
System.out.println(toString(array));
bubbleSort(array);
System.out.println();
System.out.println("Sorted array:");
System.out.println(toString(array));
}
}