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

Merge Sort

The document presents a C program that implements the merge sort algorithm to sort an array of integers. It prompts the user to enter the size and elements of the array, performs the sorting using the mergesort and merge functions, and then displays the sorted array. The output example shows the sorted result of the input array elements.

Uploaded by

deadshotlev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Merge Sort

The document presents a C program that implements the merge sort algorithm to sort an array of integers. It prompts the user to enter the size and elements of the array, performs the sorting using the mergesort and merge functions, and then displays the sorted array. The output example shows the sorted result of the input array elements.

Uploaded by

deadshotlev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

MERGE SORT:-

#include<stdio.h>
int a[10];
void main()
{
int i,n;
printf("Enter the size of an array");
scanf("%d",&n);
printf("Enter the elements of an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
printf("After sorting array elements");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int mergesort(int low,int high)
{
int mid;
if(low!=high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
int merge(int low,int mid,int high)
{
int i,j,k,temp[10];
i=low;j=mid+1;k=low;
while(i<=mid&&j<=high)
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=mid)
{
temp[k++]=a[i++];
}
while(j<=high)
{
temp[k++]=a[j++];
}
for(i=low;i<=high;i++)
{
a[i]=temp[i];
}
}

OUTPUT:-

Enter the size of an array5


Enter the elements of an array2
3
4
1
6
After sorting array elements1
2
3
4
6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy