-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbubbleSort.java
33 lines (27 loc) · 888 Bytes
/
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
package sorting_algorithms;
public class bubble_Sort {
int[] intArray = {20, 35, -15, 7, 55, 1, -22};
public static void main(String[] args) {
doSorting();
printSortedArray();
}
private static void doSorting() {
for (int lastUnsortedIndex = intArray.length - 1; lastUnsortedIndex > 0; lastUnsortedIndex--) {
for (int i = 0; i < lastUnsortedIndex; i++) {
if (i != j && intArray[i] > intArray[i + 1]) {
swapElements(intArray, i, i + 1);
}
}
}
}
private static void printSortedArray() {
for (int i = 0; i < intArray.length; i++) {
System.out.print(intArray[i] + " ");
}
}
private static void swapElements(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}