冒泡排序算法
# 什么是冒泡排序
冒泡排序(Bubble Sort),是一种最基础的交换排序算法.之所以叫冒泡排序,是因为在排序的过程中,每个排序的值像气泡一样一点点的向数组的一侧移动
# 代码实现
public class BubbleSort {
public static void main(String[] args) {
int array[] = {3, 1, 2, 5, 4};
bubbleSort(array);
System.out.println(Arrays.toString(array));
//打印结果: [1, 2, 3, 4, 5]
}
private static void bubbleSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length-1-i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
上次更新: 2023/01/04, 16:24:33
- 03
- 加密解密02-23