Thursday, October 7, 2010

MERGE SORT USING DIVIDE & CONQUER METHOD in C PROGRAMMING

ALGORITHM

MERGE SORT USING DIVIDE & CONQUER METHOD in C PROGRAMMING

#include

#include

/*
Funtion : fnMerge
Description : To Merge the B and C array in to A array
Parameters : A,B,C Arrays and their sizes
Retutn : Nothing.
*/

void fnMerge(int aiB[20],int iP,int aiC[20],int iQ,int aiA[20])
{
int iI,iJ,iK,iX,iY;
iI=0;
iJ=0;
iK=0;
while(iI
{
if(aiB[iI]
{
aiA[iK] = aiB[iI];
iI++;
}
else
{
aiA[iK]=aiC[iJ];
iJ++;
}
iK++;
}
if(iJ
for(iX=iJ;iX
aiA[iK]=aiC[iX];
else
for(iX=iI;iX
aiA[iK]=aiB[iX];
}

/*
Funtion : fnDivide
Description : To Divide the A array in to B and C array
Parameters : A Array and it size
Retutn : Nothing.
*/

void fnDivide(int aiA[20],int iN)
{
int iI,iJ,iK,aiB[20],aiC[20];
iI=0;
iJ=0;iK=0;
if(iN>1)
{
for(iI=0,iJ=0;iI
aiB[iJ]=aiA[iI];
for(iK=0;iI
aiC[iK]=aiA[iI];
fnDivide(aiB,iJ);
fnDivide(aiC,iK);
fnMerge(aiB,iJ,aiC,iK,aiA);
}
}


void main()
{
int aiA[20],iI,iN;
clrscr();
printf("\n\tMERGE SORT USING DIVIDE & CONQUER METHOD");
printf("\nEnter the Value of n : ");
scanf("%d",&iN);
printf("\nEnter the array elements \n");
for(iI=0;iI
scanf("%d",&aiA[iI]);
fnDivide(aiA,iN);
printf("\nSorted List.");
for(iI=0;iI
printf("%5d",aiA[iI]);
getch();
}


/*
OUTPUT:


MERGE SORT USING DIVIDE & CONQUER METHOD


Enter the Value of n : 7

Enter the array elements

23 45 1 -90 78 666 -12

Sorted List. -90 -12 1 23 45 78 666

*/





1 comment: