forked from till-tomorrow/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSortExample.java
38 lines (26 loc) · 983 Bytes
/
bubbleSortExample.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
import java.util.Arrays;
public class bubbleSortExample {
/*
The idea of bubble sort algorithm is to repeatedly iterate through the array and compare the adjacent
elements to each other and switch their places if they appear to be in wrong order.
Run this class to see how it works on the example numbers array.
*/
public static void bubbleSort(int[] numberArray) {
int n = numberArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (numberArray[j - 1] > numberArray[j]) {
temp = numberArray[j - 1];
numberArray[j - 1] = numberArray[j];
numberArray[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] numbers = {0, 4, 23, 5, 11, -3};
bubbleSort(numbers);
System.out.println(Arrays.toString(numbers));
}
}