Skip to content

Commit 5f2b4c8

Browse files
authored
Create BubbleSort.java
1 parent 2338428 commit 5f2b4c8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class BubbleSort {
2+
public static void main(String[] args) {
3+
int[] array = {64, 34, 25, 12, 22, 11, 90};
4+
bubbleSort(array);
5+
System.out.println("Sorted array:");
6+
for (int num : array) {
7+
System.out.print(num + " ");
8+
}
9+
}
10+
11+
public static void bubbleSort(int[] array) {
12+
int n = array.length;
13+
for (int i = 0; i < n - 1; i++) {
14+
for (int j = 0; j < n - i - 1; j++) {
15+
if (array[j] > array[j + 1]) {
16+
// Swap array[j] and array[j + 1]
17+
int temp = array[j];
18+
array[j] = array[j + 1];
19+
array[j + 1] = temp;
20+
}
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)