我觉得作者可能没弄清楚*渐进*复杂度的意思:只测一个点的速度没有任何意义,要看 N 趋近无穷时的渐进行为。
删掉文中 const arr = []一行代码,我们生成若干长为二的幂次的数组并测试,
for (var i = 12; i <= 24; ++i) { arr = []; generateArr( Math.pow(2, i) ); console.time('N = 2^' + i); quickSort(arr); console.timeEnd('N = 2^' + i); }
我得到的运行时间如下:
N = 2^12: 8.030029296875ms N = 2^13: 5.56005859375ms N = 2^14: 9.332763671875ms N = 2^15: 20.909912109375ms N = 2^16: 38.673828125ms N = 2^17: 89.992919921875ms N = 2^18: 187.841064453125ms N = 2^19: 321.177001953125ms N = 2^20: 683.663818359375ms N = 2^21: 1461.80908203125ms N = 2^22: 3129.0927734375ms N = 2^23: 6694.160888671875ms N = 2^24: 14762.876953125ms