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}.
  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.
8. Quick Sort --- An application of the linear selection algorithm_第1张图片
推导过程

The code in python3 is here

你可能感兴趣的:(8. Quick Sort --- An application of the linear selection algorithm)