-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSort.java
More file actions
29 lines (25 loc) · 926 Bytes
/
BubbleSort.java
File metadata and controls
29 lines (25 loc) · 926 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
package AlgorithmSort;
public class BubbleSort {
static int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
public static void bubbleSort(int[] list) {
boolean needNextPass = true;
for (int k = 1; k < list.length && needNextPass; k++) {
/* Array may be sorted and next pass not needed */
needNextPass = false;
for (int i = 0; i < list.length - k; i++) {
if (list[i] > list[i + 1]) {
/* Swap list[i] with list[i + 1] */
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
needNextPass = true; /* Next pass still needed */
}
}
}
}
public static void main(String[] args) {
bubbleSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}