-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBubbleSort.java
More file actions
23 lines (21 loc) · 696 Bytes
/
BubbleSort.java
File metadata and controls
23 lines (21 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = { 38, 27, 43, 3, 9, 82, 10 };
System.out.println("Unsorted array: " + Arrays.toString(arr));
bubbleSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}