Find a permutation that causes worst case of Merge Sort in C


Concept

With respect of a given set of elements, determine which permutation of these elements would result in worst case of Merge Sort?

We know,asymptotically, merge sort always consumes O(n log n) time, but the cases that need more comparisons generally consume more time in practice. Now we basically require determining a permutation of input elements that would lead to largest number of comparisons when sorted implementing a typical Merge Sort algorithm.

Example 

Consider the below set of elements as Sorted array 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Resultant input array that will result in worst case of merge sort is 11 19 15 23 13 21 17 25 12 20 16 24 14 22 18 26

Method

We examine how to get worst case input for merge sort for an input set?

Now we try to build the array in bottom up manner

Now let the sorted array be {11, 12, 13, 14, 15, 16, 17, 18}.

Now in order to build the worst case of merge sort, the merge operation that resulted in above sorted array should result in largest comparisons. As a result of this, the left and right sub-array involved in merge operation should store alternate elements of sortedarray such that, left subarray should be {11, 13, 15, 17} and right sub-array should be {12, 14, 16, 18}. So every element of array will be compared minimum once and that will result in maximumcomparisons. Now we implement the same logic for left and right sub-array as well. With respect of array {11, 13, 15, 17}, the worst case will be when its left and right sub-array are{11, 15} and {13, 17} respectively and for array {12, 14, 16, 18} the worst case will occur for {12, 14} and {16, 18}.

Complete Algorithm

GenerateWorstCase(arr[])

  • Now we create two auxillary arrays left and right and store alternate array elements in them.

  • We call GenerateWorstCase for left subarray − GenerateWorstCase (left)

  • We call GenerateWorstCase for right subarray GenerateWorstCase (right)

  • Now we copy all elements of left and right subarrays back to original array.

Example

 Live Demo

// C program to generate Worst Case of Merge Sort
#include <stdlib.h>
#include <stdio.h>
// Indicates function to print an array
void printArray(int A1[], int size1){
   for (int i = 0; i < size1; i++)
      printf("%d ", A1[i]);
   printf("
"); } // Indicates function to join left and right subarray int join(int arr1[], int left1[], int right1[], int l1, int m1, int r1){    int i; // So used in second loop    for (i = 0; i <= m1 - l1; i++)       arr1[i] = left1[i];    for (int j = 0; j < r1 - m1; j++)       arr1[i + j] = right1[j]; } // Indicates function to store alternate elemets in left // and right subarray int split(int arr1[], int left1[], int right1[], int l1, int m1, int r1){    for (int i = 0; i <= m1 - l1; i++)       left1[i] = arr1[i * 2];    for (int i = 0; i < r1 - m1; i++)       right1[i] = arr1[i * 2 + 1]; } // Indicates function to generate Worst Case of Merge Sort int generateWorstCase(int arr1[], int l1, int r1){    if (l1 < r1){       int m1 = l1 + (r1 - l1) / 2;       // creating two auxillary arrays       int left1[m1 - l1 + 1];       int right1[r1 - m1];       // Storing alternate array elements in left       // and right subarray       split(arr1, left1, right1, l1, m1, r1);       // Recursing first and second halves       generateWorstCase(left1, l1, m1);       generateWorstCase(right1, m1 + 1, r1);       // joining left and right subarray       join(arr1, left1, right1, l1, m1, r1);    } } // Driver code int main(){    // Initializes sorted array    int arr1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };    int n1 = sizeof(arr1) / sizeof(arr1[0]);    printf("Sorted array is
");    printArray(arr1, n1);    // generating worst Case of Merge Sort    generateWorstCase(arr1, 0, n1 - 1);    printf("
Input array that will result in " "worst case of merge sort is
");    printArray(arr1, n1);    return 0; }

Output

Sorted array is
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Input array that will result in worst case of merge sort is
11 19 15 23 13 21 17 25 12 20 16 24 14 22 18 26

Updated on: 23-Jul-2020

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements