Find a permutation that causes worst case of Merge Sort
Given a set of elements, find which permutation of these elements would result in worst case of Merge Sort.
Asymptotically, merge sort always takes O(n Log n) time, but the cases that require more comparisons generally take more time in practice. We basically need to find a permutation of input elements that would lead to maximum number of comparisons when sorted using a typical Merge Sort algorithm.
Example:
Consider the below set of elements
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16}
Below permutation of the set causes 153
comparisons.
{1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6,
14, 4, 12, 8, 16}
And an already sorted permutation causes
30 comparisons.
See this for a program that counts
comparisons and shows above results.
Now how to get worst case input for merge sort for an input set?
Lets us try to build the array in bottom up manner
Let the sorted array be {1,2,3,4,5,6,7,8}.
In order to generate the worst case of merge sort, the merge operation that resulted in above sorted array should result in maximum comparisons. In order to do so, the left and right sub-array involved in merge operation should store alternate elements of sorted array. i.e. left sub-array should be {1,3,5,7} and right sub-array should be {2,4,6,8}. Now every element of array will be compared at-least once and that will result in maximum comparisons. We apply the same logic for left and right sub-array as well. For array {1,3,5,7}, the worst case will be when its left and right sub-array are {1,5} and {3,7} respectively and for array {2,4,6,8} the worst case will occur for {2,4} and {6,8}.
Complete Algorithm –
GenerateWorstCase(arr[])
- Create two auxiliary arrays left and right and store alternate array elements in them.
- Call GenerateWorstCase for left subarray: GenerateWorstCase (left)
- Call GenerateWorstCase for right subarray: GenerateWorstCase (right)
- Copy all elements of left and right subarrays back to original array.
Below is the implementation of the idea
C++
#include <bits/stdc++.h>
using namespace std;
void printArray( int A[], int size)
{
for ( int i = 0; i < size; i++)
{
cout << A[i] << " " ;
}
cout << endl;
}
int join( int arr[], int left[], int right[],
int l, int m, int r)
{
int i;
for (i = 0; i <= m - l; i++)
arr[i] = left[i];
for ( int j = 0; j < r - m; j++)
{
arr[i + j] = right[j];
}
}
int split( int arr[], int left[], int right[],
int l, int m, int r)
{
for ( int i = 0; i <= m - l; i++)
left[i] = arr[i * 2];
for ( int i = 0; i < r - m; i++)
right[i] = arr[i * 2 + 1];
}
int generateWorstCase( int arr[], int l,
int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
int left[m - l + 1];
int right[r - m];
split(arr, left, right, l, m, r);
generateWorstCase(left, l, m);
generateWorstCase(right, m + 1, r);
join(arr, left, right, l, m, r);
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Sorted array is \n" ;
printArray(arr, n);
generateWorstCase(arr, 0, n - 1);
cout << "\nInput array that will result "
<< "in worst case of merge sort is \n" ;
printArray(arr, n);
return 0;
}
|
C
#include <stdlib.h>
#include <stdio.h>
void printArray( int A[], int size)
{
for ( int i = 0; i < size; i++)
printf ( "%d " , A[i]);
printf ( "\n" );
}
int join( int arr[], int left[], int right[],
int l, int m, int r)
{
int i;
for (i = 0; i <= m - l; i++)
arr[i] = left[i];
for ( int j = 0; j < r - m; j++)
arr[i + j] = right[j];
}
int split( int arr[], int left[], int right[],
int l, int m, int r)
{
for ( int i = 0; i <= m - l; i++)
left[i] = arr[i * 2];
for ( int i = 0; i < r - m; i++)
right[i] = arr[i * 2 + 1];
}
int generateWorstCase( int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
int left[m - l + 1];
int right[r - m];
split(arr, left, right, l, m, r);
generateWorstCase(left, l, m);
generateWorstCase(right, m + 1, r);
join(arr, left, right, l, m, r);
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Sorted array is \n" );
printArray(arr, n);
generateWorstCase(arr, 0, n - 1);
printf ( "\nInput array that will result in "
"worst case of merge sort is \n" );
printArray(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void join( int arr[], int left[], int right[],
int l, int m, int r)
{
int i;
for (i = 0 ; i <= m - l; i++)
arr[i] = left[i];
for ( int j = 0 ; j < r - m; j++)
arr[i + j] = right[j];
}
static void split( int arr[], int left[], int right[],
int l, int m, int r)
{
for ( int i = 0 ; i <= m - l; i++)
left[i] = arr[i * 2 ];
for ( int i = 0 ; i < r - m; i++)
right[i] = arr[i * 2 + 1 ];
}
static void generateWorstCase( int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2 ;
int [] left = new int [m - l + 1 ];
int [] right = new int [r - m];
split(arr, left, right, l, m, r);
generateWorstCase(left, l, m);
generateWorstCase(right, m + 1 , r);
join(arr, left, right, l, m, r);
}
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
10 , 11 , 12 , 13 , 14 , 15 , 16 };
int n = arr.length;
System.out.println( "Sorted array is" );
System.out.println(Arrays.toString(arr));
generateWorstCase(arr, 0 , n - 1 );
System.out.println( "\nInput array that will result in \n" +
"worst case of merge sort is \n" );
System.out.println(Arrays.toString(arr));
}
}
|
C#
using System;
class GFG {
static void join ( int []arr, int []left,
int []right, int l, int m, int r)
{
int i;
for (i = 0; i <= m - l; i++)
arr[i] = left[i];
for ( int j = 0; j < r - m; j++)
arr[i + j] = right[j];
}
static void split( int []arr, int []left,
int []right, int l, int m, int r)
{
for ( int i = 0; i <= m - l; i++)
left[i] = arr[i * 2];
for ( int i = 0; i < r - m; i++)
right[i] = arr[i * 2 + 1];
}
static void generateWorstCase( int []arr,
int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
int [] left = new int [m - l + 1];
int [] right = new int [r - m];
split(arr, left, right, l, m, r);
generateWorstCase(left, l, m);
generateWorstCase(right, m + 1, r);
join (arr, left, right, l, m, r);
}
}
public static void Main ()
{
int []arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16 };
int n = arr.Length;
Console.Write( "Sorted array is\n" );
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
generateWorstCase(arr, 0, n - 1);
Console.Write( "\nInput array that will "
+ "result in \n worst case of"
+ " merge sort is \n" );
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
<script>
function printArray(A,size)
{
for (let i = 0; i < size; i++)
{
document.write(A[i] + " " );
}
}
function join(arr,left,right,l,m,r)
{
let i;
for (i = 0; i <= m - l; i++)
arr[i] = left[i];
for (let j = 0; j < r - m; j++)
{
arr[i + j] = right[j];
}
}
function split(arr,left,right,l,m,r)
{
for (let i = 0; i <= m - l; i++)
left[i] = arr[i * 2];
for (let i = 0; i < r - m; i++)
right[i] = arr[i * 2 + 1];
}
function generateWorstCase(arr,l,r)
{
if (l < r)
{
let m = l + parseInt((r - l) / 2, 10);
let left = new Array(m - l + 1);
let right = new Array(r - m);
left.fill(0);
right.fill(0);
split(arr, left, right, l, m, r);
generateWorstCase(left, l, m);
generateWorstCase(right, m + 1, r);
join(arr, left, right, l, m, r);
}
}
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16 ];
let n = arr.length;
document.write( "Sorted array is" + "</br>" );
printArray(arr, n);
generateWorstCase(arr, 0, n - 1);
document.write( "</br>" + "Input array that will result "
+ "in worst case of merge sort is" + "</br>" );
printArray(arr, n);
</script>
|
Python3
def join(arr, left, right, l, m, r):
i = 0 ;
for i in range (m - l + 1 ):
arr[i] = left[i];
i + = 1 ;
for j in range (r - m):
arr[i + j] = right[j];
def split(arr, left, right, l, m, r):
for i in range (m - l + 1 ):
left[i] = arr[i * 2 ];
for i in range (r - m):
right[i] = arr[i * 2 + 1 ];
def generateWorstCase(arr, l, r):
if (l < r):
m = l + (r - l) / / 2 ;
left = [ 0 for i in range (m - l + 1 )];
right = [ 0 for i in range (r - m)];
split(arr, left, right, l, m, r);
generateWorstCase(left, l, m);
generateWorstCase(right, m + 1 , r);
join(arr, left, right, l, m, r);
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ];
n = len (arr);
print ( "Sorted array is" );
print (arr);
generateWorstCase(arr, 0 , n - 1 );
print ( "\nInput array that will result in \n" + "worst case of merge sort is " );
print (arr);
|
Output:
Sorted array is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Input array that will result in worst
case of merge sort is
1 9 5 13 3 11 7 15 2 10 6 14 4 12 8 16
Time Complexity: O(n logn)
Auxiliary Space: O(n)
References – Stack Overflow
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...