Improving the Quick sort

前端 未结 14 1589
终归单人心
终归单人心 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:09

    1. Choose a better pivot: eg. in median-of-three you pick 3 (random) elements and choose the pivot as the median element
    2. When length(a[]) < M (in practice choose M = 9) stop sorting. After qsort() finished apply insert sort which would take roughly M * N = O(N). This avoids many function calls close to leaf of the divide-et-impera recursion tree.

提交回复
热议问题