4
  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
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Maybe add the `language-agnostic` tag?? – Joseph Wood Dec 28 '16 at 06:20
  • @JosephWood I added C syntax – overexchange Dec 28 '16 at 06:42
  • Wiki includes quick sort in a partial list of [divide and conquer algorithms](http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm), but as you noted, in the case of quick sort some of the work is done on the current array or sub-array before division takes place. A pure merge sort doesn't do any merging until division produces two sub-arrays of size 1. Bottom up merge sort skips the recursion and starts off by dividing an array of size n into n sub-arrays of size 1. It's an issue of terminology. – rcgldr Dec 28 '16 at 06:43
  • If you think that the process of dividing must be zero cost, the quick sort doesn't do dividing at zero cost and isn't divide and conquer. However, dividing is not, AFAIK, always expected to be zero cost and I would regard quick sort as a divide and conquer algorithm. – Jonathan Leffler Dec 28 '16 at 06:45
  • @JonathanLeffler My understanding of **divide** is, to say, problem size is halved, and nothing more(no compare/swap).Problem is halved with processing or without processing, is less serious point. In the given syntax `partition(a, lo, hi)` does not half the problem in Quick sort unlike `sort(a, aux, lo, mid);` in Mergesort – overexchange Dec 28 '16 at 06:52
  • You're welcome to choose your own meaning for the language, [Humpty Dumpty](http://www.bartleby.com/73/2019.html). Not everyone will agree with what you think the words should mean. If you know the answer, why ask? I guess we should close this as 'primarily opinion-based' since there are two opinions on what the words mean. (In my view, the partitioning step does, to a first approximation, split the problem in two approximately equal parts, and each part is the processed in the same way — entirely a divide and conquer technique.) – Jonathan Leffler Dec 28 '16 at 06:56
  • Answering your questions explicitly: 1. In quick sort, the partition step is the divide process. 2. The two recursive calls are the conquer steps. By comparison, merge sort needs an extra 'undivide' (`merge`) step after the conquering in the recursive calls to `merge sort`. – Jonathan Leffler Dec 28 '16 at 07:02
  • @JonathanLeffler What is the meaning of conquer? For me, it is like gathering/merging. So, I would say, recursive call, `sort(a, lo, j-1);` syntactially half the problem(`j-1` is immediate left to first pivot element) – overexchange Dec 28 '16 at 07:12
  • I give in — what is the meaning of 'conquer'? – Jonathan Leffler Dec 28 '16 at 07:21

1 Answers1

6

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.

Community
  • 1
  • 1
Erkan Hatipoglu
  • 106
  • 2
  • 5
  • if you see the code, then quick sort is not splitting the array into 2 first. First, `partition(a, lo, hi);` partitions the elements with given pivot. Next, Splitting the array into 2 is done later by `sort(a, lo, j-1);` – overexchange Dec 28 '16 at 09:43
  • 3
    the partition operation is a splitting operation. the "combine" operation for quicksort can be thought of appending two arrays in one valid implementation of quicksort, but is unnec for "in place" sorting. – vzn Dec 28 '16 at 16:56