0% found this document useful (0 votes)
2 views3 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 entire array. The program prompts the user for the size and elements of the array, sorts them, and then outputs the sorted array.

Uploaded by

Omkar s s
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
2 views3 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 entire array. The program prompts the user for the size and elements of the array, sorts them, and then outputs the sorted array.

Uploaded by

Omkar s s
Copyright
© © All Rights Reserved
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/ 3

Merge sort

#include<stdio.h>
void merge(int *array ,int low,int mid,int high)
{
int resarray[25];
int i=low;
int k=low;
int j=mid+1;

while(i<=mid && j<=high)


{
if(array[i]<array[j])
{
resarray[k]=array[i];
i++;
k++;
}
else
{
resarray[k]=array[j];
j++;
k++;
}
}
while(i<=mid)
resarray[k++]=array[i++];
while(j<=high)
resarray[k++]=array[j++];
for(int m=low;m<=high;m++)
Merge sort

array[m]=resarray[m];
}
void sort(int *array,int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
sort(array,low,mid);
sort(array,mid+1,high);
merge(array,low,mid,high);
}
}
int main()
{
int n;
printf("Enter the size: ");
scanf("%d", &n);

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

sort(array, 0, n - 1);

printf("The sorted array is: ");


Merge sort

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


{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

Output:
Enter the size: 5
Enter the elements of array: 7
2
4
3
8
The sorted array is: 2 3 4 7 8

=== Code Execution Successful ===

You might also like