美文网首页
8. Quick Sort --- An application

8. Quick Sort --- An application

作者: 何大炮 | 来源:发表于2017-08-16 10:06 被阅读0次

    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.
    推导过程

    The code in python3 is here

    相关文章

      网友评论

          本文标题:8. Quick Sort --- An application

          本文链接:https://www.haomeiwen.com/subject/vgymrxtx.html