如何计算与插入排序混合的修改快速排序的时间复杂度?

时间:2016-10-20 12:30:59

标签: c sorting time-complexity

我知道如何单独计算快速排序和插入排序的复杂程度,但是当我将两者混合时......

以下是代码:

$value = Cache::get(md5('db-engine'.$sql), function() {
    return DB::table(...)->get();
});

return $value;

总结一下:这是一个递归的快速排序,除了当传入的矢量低于阈值时,它使用插入排序。

如何计算最佳,平均和最差情况的时间复杂度?

1 个答案:

答案 0 :(得分:2)

The part that is confusing is that when the partition gets of size 50 or less we do an insertion sort. The particular value (50) is not that important so I will switch it to 64 for a bit easier calculations. When doing the recursive formula for the average case of qsort we assume that sorting a partition of size 64 takes log(64) * 64 operations, which apparently is a constant. Note that 64 is a constant and sorting 64 elements with insert will take in the order of O(64 * 64) even in the worst case. That is still a constant. So we will only change the constant of the asymptotic behavior complexity of qsort, but we will not change the function itself.

That being said my point is that performing another algorithm below a fixed threshold may change the constant factor of your algorithm but it will not change its complexity. It will remain the same for all cases you've mentioned.