import java.util.Arrays;
public class bubbleSort{
public static void bubbleSort(int arr[]){
boolean flag = false;
for (int i = 0;i<arr.length-1;i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
flag = true;
}
if (flag = false)//数组已经达到有序状态,无需再进行循环
break;
}
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args){
int[] arr = {6,5,2,1,9,2,4,1,14,4,7,8};
bubbleSort(arr);
}
}
冒泡排序
最新推荐文章于 2023-06-06 09:14:05 发布