Quicksort with 3-way partition

前端 未结 7 1936
野趣味
野趣味 2021-01-31 15:44

What is QuickSort with a 3-way partition?

相关标签:
7条回答
  • 2021-01-31 16:33

    I think the 3-way partition is by Djstrka.

    Think about an array with elements { 3, 9, 4, 1, 2, 3, 15, 17, 25, 17 }.

    Basically you set up 3 partitions: less than, equals to, and greater than a certain pivot. The equal-to partition doesn't need further sorting because all its elements are already equal.


    For example, if we pick the first 3 as the pivot, then a 3-way partition using Dijkstra would arrange the original array and return two indices m1 and m2 such that all elements whose index is less than m1 will be lower than 3, all elements whose index is greater than or equal to m1 and less than or equal to m2 will be equal to 3, and all elements whose index is greater than m2 will be bigger than 3.

    In this particular case, the resulting array could be { 1, 2, 3, 3, 9, 4, 15, 17, 25, 17 }, and the values m1 and m2 would be m1 = 2 and m2 = 3.

    Notice that the resulting array could change depending on the strategy used to partition, but the numbers m1 and m2 would be the same.

    0 讨论(0)
提交回复
热议问题