In QuickSort, ideal situation is when median is always chosen as pivot as this results in minimum time. In this article, Merge Sort Tree is used to find median for different ranges in QuickSort and number of comparisons are analyzed.

Examples:

Input : arr = {4, 3, 5, 1, 2} Output : 11ExplanationWe have to make 11 comparisons when we apply quick sort to the array.

If we carefully analyze the quick sort algorithm then every time the array is given to the quick sort function, the array always consists of a permutation of the numbers in some range L to R. Initially, it’s [1 to N], then its [1 to pivot – 1] and [pivot + 1 to N] and so on. Also it’s not easy to observe that the relative ordering of numbers in every possible array does not change. Now in order to get the pivot element we just need to get the middle number i.e the (r – l + 2)/2^{th} number among the array.

To do this efficiently we can use a Persistent Segment Tree, a Fenwick Tree, or a Merge Sort Tree. This Article focuses on the Merge Sort Tree Implementation.

In the Modified Quick Sort Algorithm where we chose the pivot element of the array as the median of the array. Now, determining the median requires us to find the middle element considered, after sorting the array which is in itself a O(n*log(n)) operation where n is the size of the array.

Let’s say we have a range L to R then the median of this range is calculated as:

Median of A[L; R] = Middle element of sorted(A[L; R]) = (R - L + 1)/2^{th}element of sorted(A[L; R])

Let’s consider we have P partitions during the quick sort algorithm which means we have to find the pivot by sorting the array range from L to R where L and R are the starting and ending points of each partition. This is costly.

**But**, we have a permutation of numbers from L to R in every partition, so we can just find the ceil((R – L + 1)/2)^{th} smallest number in this range as we know when we would have sorted this partition then it would always would have been this element that would have ended up as being the median element as a result also the pivot. Now the elements less than pivot go to the left subtree and the ones greater than it go in the right subtree also mantaining their order.

We repeat this procedure for all partitions P and find the comparisons involved in each partition. Since in the Current Partition all the elements from L to R of that partition are compared to the pivot, we have (R – L + 1) comparisons in the current partition. We also need to consider, by recursively calculating, the total comparisons in the left and right subtrees formed too. Thus we conclude,

Total Comparisons = Comparisons in Current Partition + Comparisons in Left partition + Comparisons in right partition

We discussed above the approach to be used to find the pivot element efficiently here the

K^{th} order statistics using merge sort tree can be used to find the same as discussed.

Output:

Number of Comparisons = 11

Complexity is O(log^{2}(n)) per query for computing pivot