直接上代码
/**
*@Params :
*@Author :scy
*@Date :2019/6/20
* description:冒泡排序及其改进
*
*/
public static int[] bubbleSort(int[] array) {
int len = array.length;
int temp;
for (int i = 0;i<len-1;i++) {//循环lenth-1次
boolean isSorded = true;
for (int j = 0;j < len-1-i;j++) {//交换次数
if (array[j] > array[j+1]) {
isSorded = false;//如果交换了
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
if (isSorded) {
int num = i+1;
Log.d(TAG, "bubbleSort:发现" + num + "次时已经有序");
break;
}
}
return array;
}
//输入
int[] array = {6,3,4,5};
//输出
/com.scy.android.sortutil D/SortUtil: bubbleSort:发现2次时已经有序