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;
}
}
}
}
}
数组排序(冒泡排序)
最新推荐文章于 2023-07-14 15:39:54 发布