8. Quick Sort --- An application of the linear selection algorithm

Let a1,a2,...,an be a list of real numbers. The basic steps of Quicksort are as follows:

  1. Pick an element x as the pivot element.
  2. Partition the list into three sublists,
    R1 ={ai |ai <x}, R2 ={ai |ai =x}, andR3 ={ai |ai >x}.
  3. Sort the elements in R1 and R3 recursively, by invoking Quicksort (as the elements in R2 are already sorted.)
  4. Combine the three sorted lists R1, R2, R3 in this order into a sorted
    sequence.

評價:

  1. The running time of Quicksort is O(n^2) in the worst case.
  2. The running time is O(nlogn) in an average case: either for a random input, or using a random pivot.
  3. To make the worst case unlikely, use a random pivot or follow some rule justified by experience such as “median of three”.
  4. Small lists (less than 10–20 elements) are sorted faster by insertion sort. Therefore, use insertion sort on all small sublists rather than partitioning further.
  5. Implemented carefully,Quicksort is usually the fastest method for sorting arrays.
推導(dǎo)過程

The code in python3 is here

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容