forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortEx.java
More file actions
34 lines (23 loc) · 742 Bytes
/
BubbleSortEx.java
File metadata and controls
34 lines (23 loc) · 742 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
29
30
31
32
33
34
package com.zetcode;
import java.util.Arrays;
// Sorting an array of integers using bubble sort algorithm
public class BubbleSortEx {
public static void main(String[] args) {
int nums[] = {3, 7, 1, 15, 12, 6, 11, 9};
doBubbleSort(nums);
System.out.println(Arrays.toString(nums));
}
private static void doBubbleSort(int a[]) {
int len = a.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (a[j] > a[j + 1]) {
// swap elements
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}