Quicksort with 3-way partition

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

What is QuickSort with a 3-way partition?

7条回答
  •  孤独总比滥情好
    2021-01-31 16:16

    Picture an array:

    3, 5, 2, 7, 6, 4, 2, 8, 8, 9, 0
    

    A two partition Quick Sort would pick a value, say 4, and put every element greater than 4 on one side of the array and every element less than 4 on the other side. Like so:

    3, 2, 0, 2, 4, | 8, 7, 8, 9, 6, 5
    

    A three partition Quick Sort would pick two values to partition on and split the array up that way. Lets choose 4 and 7:

    3, 2, 0, 2, | 4, 6, 5, 7, | 8, 8, 9
    

    It is just a slight variation on the regular quick sort.

    You continue partitioning each partition until the array is sorted. The runtime is technically nlog3(n) which varies ever so slightly from regular quicksort's nlog2(n).

提交回复
热议问题