Worst time complexity of fast sorting

The best case : each selected pivot can almost divide the data into two halves, so the depth of the recursive tree is logN, so the time complexity of fast sorting isO(NlogN)

Worst case : every time the pivot is found, the array is divided into two parts, one of which is empty, so that the recursive tree becomes a slanted tree. The depth of the tree is n-1, so the time complexity becomes O(N^2).
Generally, this bad situation occurs when the data is ordered or partially ordered, such as the positive or reverse order of the array (also when the numbers are exactly the same Orderly special circumstances).
Solution:

  1. When choosing pivot, you can choose randomly instead of choosing the first or last one every time
  2. When performing fast sorting on the sub-array, you can scan it first. If the sub-array is in order, you don’t need to sort it quickly.

Guess you like

Origin blog.csdn.net/luzhensmart/article/details/112914944