Bubble & Radix Sorting test

203 views Asked by At

Im almost done with my code, but i need help with the timer for the bubble sort and radix sort. the time always on zero, i tried everything but it always ends up as zero. is it something wrong with the code. or the type of timer i am using.

update... so i fixed the time now im working on sorting test to determine if the function sorted the array... this is what i have and its still iffy. it starts showing multiple "not sorted" and "sorted" when print out.

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;


int const temp = 10000;

void bubbleSort ( int array [ temp ] );
void radixSort ( int array [ temp ] );

int main ( )
{
    int A1;
    int array [ temp ];
    char a = '\0';

    cout << "\nWould You Like To Perform Bubble and Radix Test? (y/n): ";
    cin >> a;

    if ( a == 'y' || a == 'Y' )
    {
        srand ( ( unsigned ) time ( 0 ) );

        for ( size_t i = 0; i < temp; i++ )
        {
            A1 = ( rand ( ) % temp ) + 1; 

            array [ i ] = A1;
        }

        cout << "\n-- Please Wait For Result --" << endl;

        bubbleSort ( array );
        radixSort ( array );
    }
    else
    {

    }

    return 0;
}

void bubbleSort ( int array [ temp ] )
{
    int temp1;
    time_t start, end;
    clock_t start = clock ( );

    for ( int i = 0; i < temp - 1; i++ )
    {
        for ( int j = 0; j < temp - i - 1; j++ )
        {
             if ( array [ j ] > array [ j + 1 ] )
            {
                temp1 = array [ j ];
                array [ j ] = array [ j + 1 ];
                array [ j + 1 ] = temp1;
            }
        }

    }

 for ( int s = 1; s < temp; ++s )
                {
                    if ( array [ s ] > array [ s - 1 ] )
                    {
                        cout << "sorted" << endl;
                    }
                    else
                    {
                        cout << "not sorted" << endl;
                    }
                }
   clock_t end = clock ( );

   printf ( "Sorting Time: %f seconds\n", ( double ) ( end - start ) / 1000000 );
  }

void radixSort ( int array [ temp ] )
{
     int mx = array [ temp ];
     int temp1;

     clock_t start = clock ( );

    for ( int i = 1; i < temp; i++ )
    {
        if ( array [ i ] > mx )
         {
             mx = array [ i ];

            for ( int j = 1; ( mx / j ) > 0; j *= 10 )
            {
                int out [ temp ]; //output array
                int k, count [ 10 ] = { 0 };

                for ( k = 0; k < temp; k++ )
                {
                    count [ ( array [ k ] / i ) % 10 ]++;
                }
                 for ( k = 1; k < 10; k++ )
                 {
                     count [ k ] += count [ k - 1 ];
                 }
                for ( k = i -1; k >= 0; k-- )
                {
                    out [ count [ ( array [ k ] / i ) % 10 ] - 1 ] = array [ k ];
                     count [ ( array [ k  ] / i ) % 10 ] --;
                 }
                for ( k = 0; k < temp; k++ )
                {
                    array [ k ] = out [ k ];
                }
            }
        }
    }

 for ( int s = 1; s < temp; ++s )
                {
                    if ( array [ s ] > array [ s - 1 ] )
                    {
                        cout << "sorted" << endl;
                    }
                    else
                    {
                       cout << "not sorted" << endl;
                   }
                }  

        clock_t end = clock ( );

    printf ( "Sorting Time: %f seconds\n", ( double ) ( end - start ) / 1000000 );
}
1

There are 1 answers

0
ooga On

time doesn't have good enough resolution for the task. Use clock instead, something like this:

clock_t start = clock();
// ...
clock_t end = clock();
printf("%f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);

You also may want to run the routine a number of times (100 or 1000, say) and then divide the overall time by the number of repetitions of the routine.