Merge sort is an efficient sorting algorithm that uses a divide-and-conquer approach to order elements in an array. A standard Mergesort algorithm runs in a guaranteed time, which is significantly faster than the average and worst-case running times of several other sorting algorithms.
The worst case of merge sort will be the one, where the merge sort will have to do maximum number of comparisons . Given a list containing integers 1 to 8 inclusive, how many permutations of are there such that the merge sort runs in the worst case?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The worst case of merge sort will be the one, where the merge sort will have to do maximum number of comparisons. In order to do so, the left and right sub-array involved in merge operation should store alternate elements of sorted array. Left sub-array would be ( 1 , 3 , 5 , 7 ) and right sub-array would be ( 2 , 4 , 6 , 8 ) . Every element of array will be compared at least once.
We apply the same above logic for left and right sub-array. For array ( 1 , 3 , 5 , 7 ) the worst case will be for ( 1 , 5 ) and ( 3 , 7 ) For the last pair of 2 we switch their position. so each will be compared at least once.
So ( 5 , 1 , 7 , 3 , 6 , 2 , 8 , 4 ) is a permutation that gives that gives worst case. But that is not unique because order of the left and right sub-array doesn't matter. We have 2 branches at the first divide and 4 branches at the second divide, for a total of 2 × 4 = 8 possible permutations.