Home Forums C Programming function Reply To: function

#3433
Humayan
Participant

merge sort:
 

/****************************************************************
* File Name : c:programshelpmergeSort.cpp
* Date : September,3,2008
* Comments : new project
* Compiler/Assembler :
* Program Shell Generated At: 6:27:41 p.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include < iostream >
#include < stdlib.h >
#include < time.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >
#define MAX_NUMBER 50
#define ARRAY_SIZE 20
using namespace std;

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
 void mergeSort ( int * array , int * temp , int left , int right );
 void mergeSort ( int * array );
 void merge ( int * array , int * temp , int left , int right , int rightEnd );
 void initialize ( int * array , int size );
//##################################################################################

 //main function ******************************
int main ( )
{
    int array [ ARRAY_SIZE ];
    initialize ( array , ARRAY_SIZE );
    int i = 0;
    while ( i < ARRAY_SIZE )
    {
        printf ( "%i" , array [ i ] );
        printf ( "n" );
        i ++;
    }
    mergeSort ( array );
    i = 0;
    while ( i < ARRAY_SIZE )
    {
        printf ( "%i" , array [ i ] );
        printf ( "n" );
        i ++;
    }
 return 0 ;
}

/******************************* FUNCTION DEFINITION ******************************
 Name : mergeSort
 Parameters :
  array a(n) int * ,
  temp a(n) int * ,
  left a(n) int ,
  right a(n) int

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void mergeSort ( int * array , int * temp , int left , int right )
{
    if ( left < right )
    {
        int center = ( left + right ) / 2;
        mergeSort ( array , temp , left , center );
        mergeSort ( array , temp , center + 1 , right );
        merge ( array , temp , left , center + 1 , right );
    }
    return;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : mergeSort
 Parameters :
  array a(n) int *

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void mergeSort ( int * array )
{
    int temp [ ARRAY_SIZE ];
    mergeSort ( array , temp , 0 , ARRAY_SIZE - 1 );

 return;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : merge
 Parameters :
  array a(n) int * ,
  temp a(n) int * ,
  left a(n) int ,
  right a(n) int ,
  rightEnd ( rightEnd )

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void merge ( int * array , int * temp , int left , int right , int rightEnd )
{
    int leftEnd = right - 1;
    int tempPos = left;
    int numberElements = rightEnd - left + 1;
   
    while ( left <= leftEnd && right <= rightEnd )
        if ( array [ left ] <= array [ right ] )
            temp [ tempPos ++ ] = array [ left ++ ];
        else
            temp [ tempPos ++ ] = array [ right ++ ];
    while ( left <= leftEnd )
        temp [ tempPos ++ ] = array [ left ++ ];
       
    while ( right <= rightEnd )
        temp [ tempPos ++ ] = array [ right ++ ];
   
    int i = 0;
    while ( i < numberElements )
    {
        array [ rightEnd ] = temp [ rightEnd ] ;
        rightEnd --;
    }
 
 return;
}
/******************************* FUNCTION DEFINITION ******************************
 Name : test
 Parameters :
  array a(n) int * ,
  size a(n) int

 Returns: Void type
 Comments:
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void initialize ( int * array , int size )
{
   
    srand ( time ( NULL ) );
 int i = 0;
 while ( i < size )
    {
        array [ i ] = rand () % MAX_NUMBER;
        i ++;
    } 

 return;
}