Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find a permutation that causes worst case of Merge Sort in C
Merge sort has a consistent O(n log n) time complexity, but in practice, certain input permutations require more comparisons during the merge operations. The worst case occurs when the merge operation at each level compares every element from both subarrays before completing.
Syntax
void generateWorstCase(int arr[], int left, int right);
Understanding the Worst Case
The worst case for merge sort happens when at each merge operation, elements from left and right subarrays are alternately selected. This forces maximum comparisons since we cannot skip any elements during merging.
Algorithm
To generate the worst case input from a sorted array −
- Split the sorted array into two parts: left subarray gets elements at even indices, right subarray gets elements at odd indices
- Recursively apply the same logic to both subarrays
- Merge the results back to form the worst case permutation
Example
Here's a complete program that generates the worst case input for merge sort −
#include <stdio.h>
#include <stdlib.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("<br>");
}
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];
}
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];
}
void 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[] = {11, 12, 13, 14, 15, 16, 17, 18};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original sorted array:<br>");
printArray(arr, n);
generateWorstCase(arr, 0, n - 1);
printf("\nWorst case input for merge sort:<br>");
printArray(arr, n);
return 0;
}
Original sorted array: 11 12 13 14 15 16 17 18 Worst case input for merge sort: 11 15 13 17 12 16 14 18
How It Works
The algorithm works by creating a permutation where merge operations require maximum comparisons. At each level of recursion, we distribute elements alternately between left and right subarrays, ensuring that during merge sort execution, elements from both sides will be compared as much as possible.
Key Points
- This generates the input that maximizes the number of comparisons in merge sort
- The time complexity remains O(n log n), but with maximum constant factors
- The pattern creates alternating elements that force complete traversal during merging
Conclusion
While merge sort maintains O(n log n) complexity in all cases, generating worst case inputs helps understand the algorithm's behavior and is useful for performance testing and analysis.
