Quicksort with 3-way partition

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

What is QuickSort with a 3-way partition?

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

    http://www.sorting-algorithms.com/static/QuicksortIsOptimal.pdf

    See also:

    http://www.sorting-algorithms.com/quick-sort-3-way

    I thought the interview question version was also interesting. It asks, are there four partition versions of quicksort...

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-31 16:17

    if you really grind out the math using Akra-Bazzi formula leaving the number of partitions as a parameter, and then optimize over that parameter, you'll find that e ( =2.718...) partitions gives the fastest performance. in practice, however, our language constructs, cpus, etc are all optimized for binary operations so the standard partitioning to two sets will be fastest.

    0 讨论(0)
  • 2021-01-31 16:22
      //code to implement Dijkstra 3-way partitioning
    
      package Sorting;
    
      public class QuickSortUsing3WayPartitioning {
    
    private int[]original;
    private int length;
    private int lt;
    private int gt;
    
    public QuickSortUsing3WayPartitioning(int len){
        length = len;
        //original = new int[length];
    
        original = {0,7,8,1,8,9,3,8,8,8,0,7,8,1,8,9,3,8,8,8};
    
    }
    
    public void swap(int a, int b){ //here indexes are passed
        int temp = original[a];
        original[a] = original[b];
        original[b] = temp;
    }
    
    public int random(int start,int end){
        return (start + (int)(Math.random()*(end-start+1)));
    }
    
    public void partition(int pivot, int start, int end){
        swap(pivot,start);  // swapping pivot and starting element in that subarray
    
        int pivot_value = original[start];
        lt = start;
        gt = end;
    
        int i = start;
        while(i <= gt) {
    
            if(original[i] < pivot_value) {
                swap(lt, i);
                lt++;
                i++;
            }
    
            if(original[i] > pivot_value) {
                swap(gt, i);
                gt--;
            }
            if(original[i] == pivot_value)
                i++;
        }
    }
    
    public void Sort(int start, int end){
        if(start < end) {
    
            int pivot = random(start,end); // choose the index for pivot randomly
            partition(pivot, start, end); // about index the array is partitioned
    
            Sort(start, lt-1);
            Sort(gt+1, end);
    
        }
    }
    
    public void Sort(){
        Sort(0,length-1);
    }
    
    public void disp(){
        for(int i=0; i<length;++i){
            System.out.print(original[i]+" ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
    
        QuickSortUsing3WayPartitioning qs = new QuickSortUsing3WayPartitioning(20);
        qs.disp();
    
        qs.Sort();
        qs.disp();
    
    }
    
    }
    
    0 讨论(0)
  • 2021-01-31 16:24

    3 way quick sort basically partitions the array in 3 parts. First part is lesser than the pivot , Second part is equal to pivot and third part is greater than pivot.It is linear-time partition algorithm. This partition is similar to Dutch National Flag problem.

    0 讨论(0)
  • 2021-01-31 16:30

    I think it is related to the Dijkstra way of partitioning where the partition is of elemnts smaller, equal, and larger than the pivot. Only the smaller and larger partitions have to be sorted recursively. You can see an interactive visualization and play with it at the walnut. The colors I used there are red/white/blue because the method of partitioning is usually called "the dutch flag problem"

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