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

Merge Sort Algorithm With C Code Implementation

The document outlines a C program for sorting a list of elements using the Merge Sort algorithm. It includes a clear algorithm with steps for reading input, recursively sorting sub-arrays, merging them, and displaying the sorted array. The program demonstrates the implementation of the Merge Sort function and its usage in the main function.

Uploaded by

desix88776
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 views3 pages

Merge Sort Algorithm With C Code Implementation

The document outlines a C program for sorting a list of elements using the Merge Sort algorithm. It includes a clear algorithm with steps for reading input, recursively sorting sub-arrays, merging them, and displaying the sorted array. The program demonstrates the implementation of the Merge Sort function and its usage in the main function.

Uploaded by

desix88776
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

​Merge Sort program in C​​with a​​clear algorithm​​, written in​​simple lab / exam format​​👍​

​Merge Sort in C​
​Aim​

​To write a C program to sort a list of elements using the Merge Sort algorithm.​

​Algorithm (Merge Sort)​

​ .​ S
1 ​ tart​
n​
​2.​ ​Read the number of elements​​
n​​elements into an array​
​3.​ ​Read​​
mergeSort(array, left, right)​
​4.​ ​Call​​
​5.​ ​If​​left < right​ ​:​
​○​ ​Find the middle element​
mid = (left + right) / 2​

​○​ ​Recursively apply Merge Sort on the left sub-array​
​○​ ​Recursively apply Merge Sort on the right sub-array​
merge()​
​○​ ​Merge the two sorted sub-arrays using​​
​6.​ ​Display the sorted array​
​7.​ ​Stop​

​Program: Merge Sort in C​


​#include <stdio.h>​

/​/ Function to merge two sub-arrays​


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

​int L[50], R[50];​

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


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

i​ = 0;​
​j = 0;​
​k = left;​

​while (i < n1 && j < n2) {​


​if (L[i] <= R[j])​
​arr[k++] = L[i++];​
​else​
​arr[k++] = R[j++];​
​}​

​while (i < n1)​


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

​while (j < n2)​


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

/​/ Merge Sort function​


​void mergeSort(int arr[], int left, int right) {​
​if (left < right) {​
​int mid = (left + right) / 2;​

​ ergeSort(arr, left, mid);​


m
​mergeSort(arr, mid + 1, right);​
​merge(arr, left, mid, right);​
​}​
​}​

​int main() {​
​int n, arr[50];​

​ rintf("Enter number of elements: ");​


p
​scanf("%d", &n);​

​ rintf("Enter elements:\n");​
p
​for (int i = 0; i < n; i++)​
​scanf("%d", &arr[i]);​

​mergeSort(arr, 0, n - 1);​

​printf("Sorted array:\n");​
​for (int i = 0; i < n; i++)​
​printf("%d ", arr[i]);​

​return 0;​
​}​

​Result​

​The given array is successfully sorted using the Merge Sort algorithm.​

You might also like