package com.wang.array;
import java.util.Arrays;
public class Demo07 {
public static void main(String[] args) {
int[] x = {5,92,65,48,24,22,67,65,84,1,5,82};
int[] sortmy = sortmy(x);
System.out.println(Arrays.toString(sortmy));
}
//冒泡排序
public static int[] sortmy (int[] arrays){
int temp = 0;//临时变量
//外层循环,判断走多少次
for (int i = 0; i < arrays.length-1; i++) {
boolean flag = false; //减少没有意义的比较
//内层循环,判断相邻两个数大小,如果前大于后,交换位置
for (int k = 0; k < arrays.length-1-i; k++) {
if(arrays[k]>arrays[k+1]){
temp=arrays[k];
arrays[k]=arrays[k+1];
arrays[k+1]=temp;
flag = true;
}
}
if(flag==false){ //结果为false说明在本轮外层循环中的,内层循环已经换好位置,可以提前break,结束比较
break;
}
}
return arrays;
}
}
标签:24,false,arrays,冒泡排序,int,sortmy
From: https://www.cnblogs.com/PedroPascal/p/17096848.html