//冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int[] a1 = {12, 423, 23, 41, 24, -23, 12, 546, 76}; //随意给定一个数组
for (int j = 0; j < a1.length; j++) {
for (int i = 0; i < a1.length - 1; i++) {
if (a1[i + 1] < a1[i]) { //后者小于前者,则交换位置
int temp; //设定值交换过程的中间变量
temp = a1[i];
a1[i] = a1[i + 1];
a1[i + 1] = temp;
}
}
}
System.out.println("从小到大的顺序:");
for (int k = 0; k < a1.length; k++) { //输出排好序的数组
System.out.print(a1[k] + " ");
}
}
}
Java版冒泡排序
于 2024-01-30 17:05:11 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)