Improving the Quick sort

前端 未结 14 1220
终归单人心
终归单人心 2021-02-05 22:33

If possible, how can I improve the following quick sort(performance wise). Any suggestions?

void main()
    {
      quick(a,0,n-1);
    }

    void quick(i         


        
相关标签:
14条回答
  • 2021-02-05 23:11

    Copied from my answer to answer question.

    Edit: This post assumes you already do obvious things like take advantage of tail recursion to get rid of the unnecessary call overhead.

    People like to criticize the quicksort for poor performance with certain inputs, especially when the user has control of the input. The following approach yields performance matching midpoint selection but expected complexity exponentially approaches O(n log n) as the list grows in size. In my experience it significantly outperforms best-of-3 selection due to much less overhead in the majority case. It should perform evenly with midpoint selection for "expected" inputs, but is not vulnerable to poor inputs.

    • Initialize the quicksort with a random positive integer I. That value will be used throughout the sorting process (don't have to generate multiple numbers).
    • Pivot is selected as I mod SectionSize.

    For additional performance, you should always switch your quicksort to shell sort for "small" list segments - I've seen lengths from 15-100 chosen as the cutoff.

    0 讨论(0)
  • 2021-02-05 23:20

    The wikipedia article on quicksort has a bunch of ideas.

    0 讨论(0)
提交回复
热议问题