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

Merge Sort

The document contains a C program that implements the Merge Sort algorithm to sort an array of integers. It includes functions for merging sorted subarrays and recursively sorting the array. The program prompts the user to input the number of elements and the elements themselves, then outputs the sorted array.
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 views2 pages

Merge Sort

The document contains a C program that implements the Merge Sort algorithm to sort an array of integers. It includes functions for merging sorted subarrays and recursively sorting the array. The program prompts the user to input the number of elements and the elements themselves, then outputs the sorted array.
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

#include <stdio.h>

int arr[50];

void merge(int left, int m, int right)

int i, j, k;

int temp[50];

i=left;

j=m+1;

k=left;

while(i<=m && j<=right)

if(arr[i]<arr[j])

{ temp[k]=arr[i]; i++;}

else

{ temp[k]=arr[j]; j++;}

k++;

while(i<=m)

{ temp[k]=arr[i]; i++;k++;}

while(j<=right)

{ temp[k]=arr[j]; j++;k++;}

for(i=left;i<=right;i++)

arr[i]=temp[i];

}
void mergeSort( int left, int right)

{ int m;

if (left != right)

m = (left + right ) / 2;

mergeSort( left, m);

mergeSort( m + 1, right);

merge( left, m,right);

void main()

int n,i;

printf("Number of elements to enter into an unsorted array: ");

scanf("%d",&n);

printf("Enter %d numbers\n",n);

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

mergeSort(0, n-1);

printf("the sorted numbers using MERGE SORT are \n");

for(i=0;i<n;i++)

printf("%d \t",arr[i]);

You might also like