0% found this document useful (0 votes)
3 views2 pages

Merge Sort

Merge Sort is a divide and conquer sorting algorithm that recursively divides an array into subarrays, sorts them, and merges them back into a sorted array. It has a time complexity of O(n log n) in all cases and requires O(n) extra space for merging. While it offers stable sorting and predictable performance, it is not in-place for arrays and can have recursive overhead issues in memory-constrained environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Merge Sort

Merge Sort is a divide and conquer sorting algorithm that recursively divides an array into subarrays, sorts them, and merges them back into a sorted array. It has a time complexity of O(n log n) in all cases and requires O(n) extra space for merging. While it offers stable sorting and predictable performance, it is not in-place for arrays and can have recursive overhead issues in memory-constrained environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Merge Sort

📌 Definition:
Merge Sort is a divide and conquer sorting algorithm that divides the input array into smaller subarrays, sorts them
recursively, and then merges them back into a single sorted array.
🔍 How It Works:
1. Divide: Split the array into two halves recursively until each subarray has one element.
2. Conquer:
Sort each of the subarrays (since arrays of size 1 are trivially sorted, this is done during the merge step).
3. Combine:
Merge the sorted subarrays to produce new sorted subarrays, until the final sorted array is formed.
🔁 Example Steps (Array: [38, 27, 43, 3])
1. Divide: [38, 27] and [43, 3]
2. Divide: [38] [27] and [43] [3]
3. Merge: [27, 38] and [3, 43]
4. Merge: [3, 27, 38, 43]
⏱️ Time Complexity:
Case Time
Best O(n log n)
Average O(n log n)
Worst O(n log n)
• Each division takes log n levels.
• Each level requires O(n) time to merge the elements.
💾 Space Complexity:
• O(n) extra space is required for the temporary arrays used in merging.
✅ Advantages:
• Very predictable performance: always O(n log n)
• Stable sort (preserves the order of equal elements)
• Great for sorting linked lists (in-place version exists)
⚠️ Disadvantages:
• Uses additional memory (not in-place for arrays)
• Recursive overhead can be an issue for large arrays in memory-constrained environments
Implementation of Merge Sort in C
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

int L[n1], R[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

void printArray(int A[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
return 0;
}

You might also like