package Session4;
//冒泡排序
import java.util.Arrays;
public class Demo05_BubbleSort {
public static void main(String[] args) {
long start = System.currentTimeMillis();
int[] a = {1, 55, 99, 88, 4, 5, 588, 12, 51, 555, 9999};
int[] sort = sort(a);
System.out.println(Arrays.toString(sort));//此处写要测试的代码
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - start) + "毫秒");
}
public static int[] sort(int[] array) {
int temp = 0;
boolean flag = false;
for (int i = 0; i < array.length - 1; i++) {//外层循环,遍历数组所有原数,判断我们要走多少次
for (int j = 0; j < array.length - 1 - i; j++) {//内层循环,比较两个数,如果第一个数比第二个数大,则交换位置
if (array[j + 1] < array[j]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
flag = true;
}
}
if (flag = false) {//减少判断次数
break;
}
}
return array;
}
}
Day14-BubbleSort(冒泡函数)
最新推荐文章于 2023-01-26 17:24:50 发布