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
Selected Reading
C program to sort an array by using merge sort
Merge sort is a divide-and-conquer algorithm that recursively divides an array into two halves, sorts each half separately, and then merges the sorted halves back together. It guarantees O(n log n) time complexity in all cases.
Syntax
void MergeSort(int array[], int left, int right); void Merge(int array[], int left, int middle, int right);
How Merge Sort Works
The merge sort algorithm follows these steps −
- Divide: Split the array into two halves at the middle point
- Conquer: Recursively sort both halves
- Combine: Merge the two sorted halves into a single sorted array
Example: Complete Merge Sort Implementation
Here is a complete C program demonstrating merge sort with predefined array values −
#include <stdio.h>
void Merge(int array[], int left, int middle, int right) {
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
/* Create temporary arrays */
int leftArray[n1], rightArray[n2];
/* Copy data to temporary arrays */
for (i = 0; i < n1; i++)
leftArray[i] = array[left + i];
for (j = 0; j < n2; j++)
rightArray[j] = array[middle + 1 + j];
/* Merge the temporary arrays back into array[left..right] */
i = 0; /* Initial index of first subarray */
j = 0; /* Initial index of second subarray */
k = left; /* Initial index of merged subarray */
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}
/* Copy the remaining elements of leftArray[], if any */
while (i < n1) {
array[k] = leftArray[i];
i++;
k++;
}
/* Copy the remaining elements of rightArray[], if any */
while (j < n2) {
array[k] = rightArray[j];
j++;
k++;
}
}
void MergeSort(int array[], int left, int right) {
if (left < right) {
int middle = left + (right - left) / 2;
/* Sort first and second halves */
MergeSort(array, left, middle);
MergeSort(array, middle + 1, right);
/* Merge the sorted halves */
Merge(array, left, middle, right);
}
}
void printArray(int array[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
printf("<br>");
}
int main() {
int array[] = {64, 34, 25, 12, 22, 11, 90, 5};
int size = sizeof(array) / sizeof(array[0]);
printf("Original array: ");
printArray(array, size);
MergeSort(array, 0, size - 1);
printf("Sorted array: ");
printArray(array, size);
return 0;
}
Original array: 64 34 25 12 22 11 90 5 Sorted array: 5 11 12 22 25 34 64 90
Key Points
- Time Complexity: O(n log n) in all cases (best, average, worst)
- Space Complexity: O(n) due to temporary arrays used in merging
- Stable: Maintains relative order of equal elements
- Not In-place: Requires additional memory for temporary arrays
Conclusion
Merge sort is an efficient, stable sorting algorithm with consistent O(n log n) performance. Though it requires extra memory, its predictable behavior makes it ideal for applications requiring guaranteed performance.
Advertisements
