Computer Programming web Web programming Tips



The Recursive Binary Merge Sort: C++ source code

By Sergey Skudaev

Happy Amazon Prime Day July 12 - Exclusive Deals for Prime Members - Start Free 30-Day Trial




Recursive Binary Merge Sort, implementation in C++
Date:3/26/2000   

#include <iostream.h>
#include <toolhelp.h>
     //GetTickCount()  is valid only for Borland C++


void printArray ( int array [], int last );
void recursive_bin_merge ( int array [], int last, int &cells );
void resetArray(int array[], int last);
void merge(int array[], int listUp[], int listDn[], int index, int cells,              
                                                                                 int idxUp, int  idxDn);
void main( )
{
    int array[] = {75, 55, 15, 20, 85, 30, 35, 10, 60, 40, 50, 25, 45, 80, 70, 65};
    int last = 16;
    int cells = 1;
    long lstart= 0;
    long lend = 0;
                                printArray ( array, last  );

                                lstart = GetTickCount( );

                                recursive_bin_merge ( array, last, cells);

                                lend = GetTickCount();

                                int takes;
                                takes = lend - lstart;

    cout<<"\nIt takes "<<takes<<" msec to sort the array of 16 integers"<<endl;
}



void recursive_bin_merge(int array[], int last, int &cells)
{
        int current = 0;
        int new_current = 0;
        int listUp[]= {0,0,0,0,0,0,0,0};
        int listLow[]= {0,0,0,0,0,0,0,0};

        if (cells <= last/2)
        {
                while (current < last)
                {
                        for ( int i = 0; i< cells; i ++)
                        listUp [new_current + i] = array [current + i];

                     current = current + cells;

                        for (int j = 0; j<cells; j++)
                        listLow [new_current+j] = array [current + j];

                    current = current + cells;

                    new_current = new_current + cells;
                }

                            printArray ( listUp, 8);
                            printArray ( listLow, 8);

                            resetArray ( array, last);


                            int index =0;
                            int count = cells;
                            int idxUp = 0;
                            int idxDn = 0;
                            int t = 0;

                        while (index < last)
                        {
                            t = t+1;
                                merge(array, listUp, listLow, index, count, idxUp, idxDn);
                                index = index + 2*cells;
                                idxUp = idxUp + cells;
                                idxDn = idxDn + cells;
                                count = count + cells;

                            }

                            cells = cells * 2;
                            cout<<"Merge_Times :"<<t<<endl;

                            printArray(array, last);

                            recursive_bin_merge( array, last, cells);

            } //end if
}

void printArray(int array[],int last)
{
    for ( int i = 0; i < last; i++)

    cout<<array[i]<<",";

    cout<<endl;
}



void resetArray( int array[], int last)
{
        for ( int i = 0; i< last;i++ )

        array[i] = 0;
}




void merge ( int array[], int listUp[], int listDn[], int index, int cells, 

                                                                                        int idxUp, int idxDn)
{


while ((idxUp < cells)&&(idxDn < cells))
{

            if ( listUp [idxUp] < listDn [idxDn] )
            {
            array [index] = listUp [idxUp];
            index++;
            idxUp++;
            }
            else
            {
            array [index] = listDn [idxDn];
            index++;
            idxDn++;
            }

        }

        while (idxUp < cells)
        {
        array [index] = listUp [idxUp];
        index++;
        idxUp++;
        }

        while (idxDn < cells)
        {
        array [index] = listDn [idxDn];
        index++;
        idxDn++;
        }

}

Demo Applet

Subscribe to our mail list to receive free code examples.

Comments

No comments yet.

Add Comment

* Required information
(never displayed)
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
 
1000
Enter the fifth word of this sentence.
 
Enter answer:
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form inputs on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
 
Powered by Commentics

My eBooks on Amazon.com

Learn C++ Programming By Examples

Learn SQL Programming By Examples

Learn PHP Programming by Examples

Visual Basic Programming By Examples

How to Build Your Own Web Site from Scratch