RSS

Merge Sort

08 Mar

C Program for implementing merge-sort

#include<conio.h>
#include<stdio.h>
#define MAX 5

int a[MAX],i;
void meage_sort(int a[],int,int);
void split(int a[],int,int,int,int);
void main()
{
clrscr();
for (int i=0;i<MAX;i++)
{
printf(“\n enter %d element “,i+1);
scanf(“%d”,&a[i]);
}
meage_sort(a,0,MAX-1);
printf(“\n UR DATA AFTER SORTING\n “);
for(i=0;i<MAX;i++)
{
printf(“%d->”,a[i]);
}
getch();
}

void meage_sort(int a[],int lower, int upper)
{
int mid;

if(lower<upper)
{
mid=(lower+upper)/2;
meage_sort(a,lower,mid);
meage_sort(a,mid+1,upper);
split(a,lower,mid,mid+1,upper);
}
}
void split(int a[],int low1,int up1,int low2,int up2)
{
int p,q,j,n;
int temp[MAX];
p=low1;
q=low2;
n=0;
while((p<=up1) && (q<=up2))
temp[n++]=(a[p] < a[q] ? a[p++]:a[q++]);
while(p<=up1)
temp[n++]=a[p++];
while(q<=up2)
temp[n++]=a[q++];

for(q=low1,n=0 ;q<=up1 ;q++,n++)
a[q]=temp[n];
for(q=low2,j=n ;q<=up2 ;q++,j++)
a[q]=temp[j];
}

 

Advertisements
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: