We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
#include <stdio.
h> #include <stdlib.h>
// Merges two subarrays of arr[].
// First subarray is arr[left..mid] // Second subarray is arr[mid+1..right] void merge(int arr[], int left, int mid, int right) { int i, j, k; int n1 = mid - left + 1; int n2 = right - mid;
// Create temporary arrays
int leftArr[n1], rightArr[n2];
// Copy data to temporary arrays
for (i = 0; i < n1; i++) leftArr[i] = arr[left + i]; for (j = 0; j < n2; j++) rightArr[j] = arr[mid + 1 + j];
// Merge the temporary arrays back into arr[left..right]
i = 0; j = 0; k = left; while (i < n1 && j < n2) { if (leftArr[i] <= rightArr[j]) { arr[k] = leftArr[i]; i++; } else { arr[k] = rightArr[j]; j++; } k++; }
// Copy the remaining elements of leftArr[], if any
while (i < n1) { arr[k] = leftArr[i]; i++; k++; }
// Copy the remaining elements of rightArr[], if any