-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathBubbleSort.java
More file actions
29 lines (26 loc) · 871 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
29 lines (26 loc) · 871 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
// Bubble Sort Algorithm in Java
// Time Complexity: O(n^2)
// Simple algorithm that repeatedly swaps adjacent elements if they are in wrong order.
public class BubbleSort {
public static void sort(int[] arr) {
int n = arr.length;
// Outer loop for passes
for (int i = 0; i < n - 1; i++) {
// Inner loop for comparison
for (int j = 0; j < n - i - 1; j++) {
// Swap if current element is greater than the next
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] data = {5, 3, 8, 4, 2};
sort(data);
for (int num : data)
System.out.print(num + " ");
}
}