forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.java
31 lines (26 loc) · 837 Bytes
/
bubble.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
public class Bubble {
static void bubbleSort(int[] arr) {
for (int r = arr.length - 1; r > 0; r--) {
for (int i = 0; i < r; i++) {
if(arr[i] > arr[i + 1]) {
int tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
}
public static void main(String[] args) {
int[] test = new int[]{20, -3, 50, 1, -6, 59};
System.out.println("Unsorted array :");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + " ");
}
bubbleSort(test);
System.out.println("\n\nSorted array :");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + " ");
}
System.out.println("");
}
}