import java.text.SimpleDateFormat;
import java.util.Date;
public class BubbleSort {
public static void main(String[] args) {
int[] array = new int[80000];
for (int i = 0; i < 80000; i++) {
array[i] = (int)(Math.random()*8000000);
}
Date start = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("start :"+simpleDateFormat.format(start));
bubbleSort(array);
Date end = new Date();
System.out.println("end :"+simpleDateFormat.format(end));
}
public static int[] bubbleSort(int[] array) {
int len = array.length;
boolean flag = false; //判断是否发生过排序
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (array[j] > array[j + 1]) {
array[j] = array[j] + array[j+1] - (array[j+1] = array[j]);
flag = true;
}
}
if (!flag) {
break;
}
flag = false;
}
return array;
}
}
测试冒泡排序算法对80000个数据进行排序所花费的时间
最新推荐文章于 2025-05-18 21:10:09 发布