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,
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,
Questions:
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?
My understanding of Conquer phase mean, gathering/merging back. In quick sort, Where does conquer phase mean?
Note: Am a beginner, in sorting algorithms
Divide & Conquer algorithms have 3 stages:
For merge sort (http://www.cs.umd.edu/~meesh/351/mount/lectures/lect6-divide-conquer-mergesort.pdf):
For quick sort (https://www.cs.rochester.edu/~gildea/csc282/slides/C07-quicksort.pdf):
Note: Thanks to University of Rochester and University of Maryland CS departments.