Improving the Quick sort

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

    The first thing to do is benchmark it. And benchmark it against the standard qsort, and against the c++ std::sort and std::stable_sort.

    Your results, should your data-set be big enough, will show that the std::sort is superior to qsort in all cases except those where the std::stable_sort is superior. This is because the std::sort is templated, and therefore the comparision can be inlined.

    Your code inlines the comparison because its not generic. If your code is slower than qsort, you have a problem right there.

    The faster sort would be to sort parts in parallel, e.g. openmp, and then merge them back together.

提交回复
热议问题