// Program for Merge Sort
//For Merge sort two arrays or record files to be Merged must be in Order
#include <iostream.h>
#include <conio.h>
//----------------------------------------------------------------------
void Bubblesort ( int array[] , int UB )
{
for ( int i = 0 ; i <= UB ; i++ )
for ( int j=0 ; j<=UB-1 ; j++ )
{
if ( array [j] > array [j+1] )
{
int temp = array [j] ;
array [j] = array[j+1] ;
array [j+1] = temp ;
}
}
}
//-------------------------------------------------------------------
void Mergesort ( int a[] , int b[] , int c[] , int n1 , int n2 , int n3 )
{
int alimit , blimit , climit ;
int apoint , bpoint , cpoint ;
alimit = n1 - 1 ;
blimit = n2 - 1 ;
climit = n3 - 1 ;
apoint = 0 ;
bpoint = 0 ;
for ( cpoint = 0 ; apoint <= alimit && bpoint <= blimit ; cpoint++ )
{
if ( a[apoint] < b[bpoint] )
c[cpoint] = a[apoint++] ;
else
c[cpoint] = b[bpoint++] ;
}
while ( apoint <= alimit && cpoint <= climit )
c[cpoint++] = a[apoint++];
while ( bpoint <= blimit && cpoint <= climit )
c[cpoint++] = b[bpoint++];
}
void Showarray ( int array[] , int UB )
{
gotoxy ( 1,2 ) ;
cout << "\n" ;
for ( int i = 0 ; i < UB ; i++ )
{
cout << array [i] << endl ;
}
}
//-------------------------------------------------------------------
void main ()
{
clrscr ();
int array1 [5] = { 42 , 23 , 74 , 11 , 65 };
int array2 [5] = { 58 , 94 , 36 , 99 , 87 };
int array3 [10] ;
Bubblesort ( array1 , 4 ) ;
Bubblesort ( array2 , 4 ) ;
Mergesort ( array1 , array2 , array3 , 5 , 5 , 10 ) ;
Showarray ( array3 , 10 ) ;
getch ();
}
Sunday, March 15, 2009
C Program Merge Sort
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment