-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSorting.java
More file actions
28 lines (22 loc) · 891 Bytes
/
Copy pathBubbleSorting.java
File metadata and controls
28 lines (22 loc) · 891 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
import java.util.Arrays;
public class BubbleSorting {
public static void main(String[] args) {
int[] inputArr = {5, 13, 3, 8, 4, 2};
bubbleSort(inputArr);
System.out.println("Sorted Array: " + Arrays.toString(inputArr));
}
public static void bubbleSort(int[] inputArr) {
boolean swapped;
do {
swapped = false;
for (int index = 0; index < inputArr.length - 1; index++) {
if (inputArr[index] > inputArr[index + 1]) {
inputArr[index] = inputArr[index] + inputArr[index + 1];
inputArr[index + 1] = inputArr[index] - inputArr[index + 1];
inputArr[index] = inputArr[index] - inputArr[index + 1];
swapped = true;
}
}
} while (swapped);
}
}