0% found this document useful (0 votes)
4 views3 pages

Merge Sorting

The document contains a C program that implements the merge sort algorithm. It includes functions to merge subarrays, recursively sort an array, and print the sorted array. The program prompts the user for the number of elements and their values, sorts the array using merge sort, and then displays the sorted result.

Uploaded by

athravasade
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)
4 views3 pages

Merge Sorting

The document contains a C program that implements the merge sort algorithm. It includes functions to merge subarrays, recursively sort an array, and print the sorted array. The program prompts the user for the number of elements and their values, sorts the array using merge sort, and then displays the sorted result.

Uploaded by

athravasade
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/ 3

Experiment 3

Program -

#include <stdio.h>

// Function to merge two subarrays into one sorted array


void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temporary arrays to hold the left and right subarrays


int leftArr[n1], rightArr[n2];

// Copy data to temporary arrays leftArr[] and rightArr[]


for (int i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (int i = 0; i < n2; i++)
rightArr[i] = arr[mid + 1 + i];

int i = 0; // Initial index of left subarray


int j = 0; // Initial index of right subarray
int k = left; // Initial index to merge into original array

// Merge the temp arrays back into the original array


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


while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
Experiment 3
}

// Function to implement merge sort recursively


void mergeSort(int arr[], int left, int right) {
if (left < right) {
// Find the middle point
int mid = left + (right - left) / 2;

// Sort the first and second halves


mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);

// Merge the sorted halves


merge(arr, left, mid, right);
}
}

// Function to print an array


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

int main() {
int n;

// User input for size of the array


printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

// User input for the array elements


printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Call mergeSort to sort the array


mergeSort(arr, 0, n - 1);

// Print the sorted array


printf("Sorted array: ");
printArray(arr, n);

return 0;
}
Experiment 3

Output -

You might also like