数据量在50以内的时候,冒泡和插入会比归并和快速的用时少一些。但是在数据量超过50时,差距就很明显了。
当然排序算法的效率也会和数组本身的排序有关,但是影响不会达到倍数级别。所以随机数组的测试也有一定的参考价值。
先上代码,再看截图。
(如有问题,欢迎指正)
package sort;
import java.util.Random;
/***
*
* @author Easily
*
*/
public class AllSort {
static Random random = new Random();
/**
* 冒泡 两两比较交换。大泡往上,小泡往下
*
* @param a
*/
public static int[] maopao(int[] a) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
// System.out.println(a[j]);
}
//System.out.print(a[j] + " ");
}
//System.out.println("");
}
return a;
}
/**
* 插入排序 选最小的,放到当前的第一个位置。
*
* @param a
*/
public static int[] charu(int[] a)