排序算法 之 效率测试

前面我们写了常见的几种排序算法,并分析了各种算法的实现思想,及时间复杂度等情况,但由于只是分析,木有实际的数据做对比测试,所以对各个算法的效率也没有一个明确的概念,下面我们就通过具体的测试来看看同算法之间的效率差距。

声明11个长度为100的元素取值范围为0到1000的序列

int length = 100;
int[] testArray1 = new int[length];
int[] testArray2 = new int[length];
int[] testArray3 = new int[length];
int[] testArray4 = new int[length];
int[] testArray5 = new int[length];
int[] testArray6 = new int[length];
int[] testArray7 = new int[length];
int[] testArray8 = new int[length];
int[] testArray9 = new int[length];
int[] testArray10 = new int[length];
int[] testArray11 = new int[length];
Random random = new Random();

for (int i = 0; i < length; i++)
{
    int temp = random.Next(0,1000);
    testArray1[i] = temp;
    testArray2[i] = temp;
    testArray3[i] = temp;
    testArray4[i] = temp;
    testArray5[i] = temp;
    testArray6[i] = temp;
    testArray7[i] = temp;
    testArray8[i] = temp;
    testArray9[i] = temp;
    testArray10[i] = temp;
    testArray11[i] = temp;
}

运行测试,测试结果截图:

1

乍一看,几种算法之间效率好像木有差别,设置前几种时间复杂度比较高的算法还要快一点。别急,让我们增加序列中的元素数量再试一下,把length =100改为length =1000再次运行测试,结果截图:

2

结果已经可以看出,时间复杂度低的算法是领先的,但效果好像不太明显。让我们把把length =1000改为length =10000再次运行测试,测试结果截图:

3

这次测试结果已经有了质的改变,相信通过测试大家对不同时间复杂度算法之间的效率已经有了一个清晰的概念。

转载于:https://my.oschina.net/secyaher/blog/274437

猜你喜欢

转载自blog.csdn.net/weixin_33928467/article/details/91966958