Is Quick Sort a Divide & Conquer approach?

3.1k views Asked by At
  1. I consider Merge sort as divide and conquer because,

    Divide - Array is literally divided into sub arrays without any processing(compare/swap), and the problem sized is halved/Quartered/....

    Conquer - merge() those sub arrays by processing(compare/swap)

    Code gives an impression that it is Divide&Conquer,

    if(hi <= lo) return;
    int mid = lo + (hi-lo)/2; //No (compare/swap) on elements before divide
    sort(a, aux, lo, mid); // Problem is halved(Divide)
    sort(a, aux, mid+1, hi);
    merge(a, aux, lo, mid); // (Compare/swap) happens here during Merge - Conquer
    

Merge sort trace says, that problem is granulated and then processed,

enter image description here

  1. But in Quick sort,

    Firstly, Complete array is processed(compare/swap) using a partition element(pivot) and fix the final position of pivot, and then problem size is halved/Quartered/.... for re-partitioning,

    Code does not give an impression of divide & conquer,

    if(hi <= lo) return;
    int j = partition(a, lo, hi); // Do you call this divide phase?
    sort(a, lo, j-1);  // This looks like divide phase, because problem is halved
    sort(a, j+1, hi);
    

Quick sort trace, shows that processing is started on complete array and then reaches granular level,

enter image description here

Questions:

  1. My understanding of Divide phase mean, reducing(half) the problem size. In Quick sort, do you consider processing(compare/swap) array and partition using pivot, as Divide phase?

  2. My understanding of Conquer phase mean, gathering/merging back. In quick sort, Where does conquer phase mean?


            Note: Am a beginner, in sorting algorithms
1

There are 1 answers

2
Erkan Hatipoglu On

Divide & Conquer algorithms have 3 stages:

  1. Divide,
  2. Conquer,
  3. Combine,

For merge sort (http://www.cs.umd.edu/~meesh/351/mount/lectures/lect6-divide-conquer-mergesort.pdf):

  1. Divide: Split the array into 2 subarrays,
  2. Conquer: Merge sort the resulting subarrays recursively,
  3. Combine: Merge the two sorted subarrays into a single sorted list.

For quick sort (https://www.cs.rochester.edu/~gildea/csc282/slides/C07-quicksort.pdf):

  1. Divide: First rearrange then split the array into 2 subarrays,
  2. Conquer: Quick sort the resulting subarrays recursively,
  3. Combine: None.

Note: Thanks to University of Rochester and University of Maryland CS departments.