-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.java
42 lines (35 loc) · 1.2 KB
/
BubbleSort.java
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
package sorting_searching;
import java.util.Comparator;
public class BubbleSort {
public static void main(String[] args) {
// Example:
int[] array = {91, 34, 86, 39, 27, 87, 70, 48, 100, 95};
bubbleSort(array);
System.out.println(java.util.Arrays.toString(array)); // [27, 34, 39, 48, 70, 86, 87, 91, 95, 100]
}
public static void bubbleSort(int[] a) {
for (int j = 0; j < a.length - 1; ++j) {
for (int i = 0; i < a.length - j - 1; ++i) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}
public static <T extends Comparable<? super T>> void bubbleSort(T[] a) {
bubbleSort(a, Comparator.naturalOrder());
}
public static <T> void bubbleSort(T[] a, Comparator<T> comp) {
for (int j = 0; j < a.length - 1; ++j) {
for (int i = 0; i < a.length - j - 1; ++i) {
if (comp.compare(a[i], a[i + 1]) > 0) {
T temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}
}