package com.test;
import java.util.Arrays;
public class Test {
public static void main(String[] args){
int[] arr = {1,8,3,5,4,6,2};
bubbleSort(arr); //调用冒泡排序方法
System.out.println(Arrays.toString(arr)); //打印输出结果
}
//冒泡排序方法
public static void bubbleSort(int[] arr){
for (int i = 0;i < arr.length;i++) { //遍历数组每一个元素
for (int bound = arr.length - 1; bound > i; bound--) { //遍历还未排过序的数组元素
if (arr[bound] > arr[bound - 1]) { //大数向前排,若数组前一个元素的值比后一个的小
//交换数组元素
int temp;
temp = arr[bound];
arr[bound] = arr[bound - 1];
arr[bound - 1] = temp;
}
}
}
}
}
数组排序(冒泡排序)
最新推荐文章于 2022-07-28 19:24:26 发布
本文深入探讨了冒泡排序算法的实现原理,通过一个具体的Java示例代码展示了如何使用冒泡排序对整型数组进行升序排列。文章详细解释了双重循环在冒泡排序中的作用,以及如何通过比较和交换操作完成排序过程。
381

被折叠的 条评论
为什么被折叠?



