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
归并排序的过程是将问题分解并逐个处理。
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);
快速排序跟踪,显示处理从完整数组开始,然后达到细粒度水平。
问题:
Divide阶段的理解是指减少(一半)问题规模。在快速排序中,您是否将使用枢轴对数组进行处理(比较/交换)并分区视为Divide阶段?
Conquer阶段的理解是指收集/合并回来。在快速排序中,conquer阶段是什么意思?
Note: Am a beginner, in sorting algorithms
merge
)步骤。 - Jonathan Lefflersort(a, lo, j-1);
在语法上解决了一半的问题(j-1
是第一个枢轴元素的左侧立即位置)。 - overexchange