F How to implement Merge Sort using C ? | CodeTheta

How to implement Merge Sort using C ?

May 18, 2013

This merge sort program is written in c programming. In this programming code various functions are included. check those functions and understand them those calling method.

/*
 Program to implement MERGE SORT using C.
 ----------------------------------------
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void merge_sort(float*,int,int,int);
void merge_pass(float*,int,int);
void main()
{
float list[1000];
int i,n = 30;
clrscr();
printf("\n Size of the list: %d", n);
for(i=0;i<n;i++)
 list[i]=(float)(rand()%100);
printf("\n Entered list as follows:\n");
for(i=0;i<n;i++)
printf(" %d ",(int)list[i]);
i=0 ;
merge_pass(list,i,n-1);
printf("\n\n Merge sorted list is as follows:\n");
for(i=0;i<n;i++)
printf(" %d",(int) list[i]);
getch();
}

//MERGE SORT
void merge_sort(float l[],int top,int size,int bottom)
 {
 float temp[1000];
 int f=top;
 int s=size+1;
 int t=top;
 int upper;
 while((f<=size)&&(s <=bottom))
 {
 if(l[f]<=l[s])
 {
 temp[t]=l[f] ;
 f++;
 }
 else
 {
 temp[t]=l[s] ;
 s++;
 }
 t++;
 }
 if(f<=size)
 {
 for(;f<=size;f++)
 {
 temp[t]=l[f];
 t++;
 }
 }
 else
 {
 for(s=s;s<=bottom;s++)
 {
 temp[t]=l[s];
 t++;
 }
 }
 for(upper=top;upper<=bottom;upper++)
 l[upper]=temp[upper];
 }

void merge_pass(float append[],int m,int n)
 {
 if(m!=n)
 {
 int mid=(m+n)/2;
 merge_pass(append,m,mid);
 merge_pass(append,mid+1,n);
 merge_sort( append,m,mid,n);
 }
 }

 Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us

Post a Comment