Write an algorithm or source code for merge sort using c language?

#include<stdio.h>

#include<conio.h>

void mergesort(int[],int ,int);

void merge(int[],int,int,int);

void main()

{

int a[10] = {10,9,7,10,23,63,29,11,02,83};

int i;

mergesort(a,0,9);

printf(“printing the sorted elements”);

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

{

printf(“\n printing the sorted elements’);

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

{

printf(“\n %d\n”,a[i]);

}

 

}

void mergesort(int a[],int beg,int end)

{

int mid;

if(beg<end)

{

mid= (beg+end)/2;

mergesort(a,beg,mid);

mergesort(a,mid+1,end);

merge(a,beg,mid,end);

 

 

}

 

}

void merge(int a[],int beg,int mid,int end)

{

int i =beg,j=mid+1;,k,index = beg;

int temp[10];

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

{

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

{

temp[index] = a[i];

i = i+1;

}

else

{

temp[index]= a[j];

j=j+1;

}

index++;

 

 

}

if(i> mid)

{

while(j<= end)

{

temp[index] = a[j];

index++;

j++;

 

}

}

else

{

while(i< =mid)

{

temp[index] = a[i];

index++;

i++;

 

}

}

k=beg;

while(k<index)

{

a[k]= temp[k];

k++;

 

}

}

 

getch();

}

output:———————–

printing the sorted elements:-

02,07,9,10,10,11,23,29,63,83

 

Leave a Reply

Your email address will not be published. Required fields are marked *