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


Suppose we have a set of elements; we have to find which permutation of these elements would result in worst case of Merge Sort? As we know asymptotically, merge sort always consumes O (n log n) time, but some cases need more comparisons and consumes more time. Here we have to find a permutation of input elements that will require higher number of comparisons when sorted implementing a typical Merge Sort algorithm.

So, if the input is like [11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26] , then the output will be [11,19,15,23,13,21,17,25,12,20,16,24,14,22,18,26].

To solve this, we will follow these steps −

  • Define a function merge(), this will take array arr, array left, array right, l_index, m_index, r_index,
  • for initialize i := 0, when i <= m_index - l_index, update (increase i by 1), do −
    • arr[i] := left[i]
  • for initialize j := 0, when j < r_index - m_index, update (increase j by 1), do −
    • arr[i + j] = right[j]
  • Define a function divide(), this will take array arr, array left, array right, l_index, m_index, r_index,
  • for initialize i := 0, when i <= m_index - l_index, update (increase i by 1), do −
    • left[i] := arr[i * 2]
  • for initialize i := 0, when i < r_index - m_index, update (increase i by 1), do −
    • right[i] := arr[i * 2 + 1]
  • Define a function gen_worst_seq(), this will take array arr[], l_index, r_index,
  • if l_index < r_index, then −
    • m_index := l_index + (r_index - l_index) / 2
    • Define an array left of size: m_index-l_index+1.
    • Define an array right of size: r_index-m_index.
    • divide(arr, left, right, l_index, m_index, r_index)
    • gen_worst_seq(left, l_index, m_index)
    • gen_worst_seq(right, m_index + 1, r_index)
    • merge(arr, left, right, l_index, m_index, r_index)

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void display(int A[], int size) {
   for (int i = 0; i < size; i++)
      cout << A[i] << " ";
   cout << endl;
}
int merge(int arr[], int left[], int right[],int l_index, int m_index, int r_index) {
   int i;
   for (i = 0; i <= m_index - l_index; i++)
      arr[i] = left[i];
   for (int j = 0; j < r_index - m_index; j++)
      arr[i + j] = right[j];
}
int divide(int arr[], int left[], int right[], int l_index, int m_index, int r_index) {
   for (int i = 0; i <= m_index - l_index; i++)
      left[i] = arr[i * 2];
   for (int i = 0; i < r_index - m_index; i++)
      right[i] = arr[i * 2 + 1];
}
int gen_worst_seq(int arr[], int l_index, int r_index) {
   if (l_index < r_index) {
      int m_index = l_index + (r_index - l_index) / 2;
      int left[m_index - l_index + 1];
      int right[r_index - m_index];
      divide(arr, left, right, l_index, m_index, r_index);
      gen_worst_seq(left, l_index, m_index);
      gen_worst_seq(right, m_index + 1, r_index);
      merge(arr, left, right, l_index, m_index, r_index);
   }
}
int main() {
   int arr[] = {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
   int n = sizeof(arr) / sizeof(arr[0]);
   gen_worst_seq(arr, 0, n - 1);
   display(arr, n);
}

Input

11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

Output

11 19 15 23 13 21 17 25 12 20 16 24 14 22 18 26

Updated on: 28-Aug-2020

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements