泡泡&基数排序测试

时间:2015-06-11 00:36:19

标签: c++ arrays timer bubble-sort radix-sort

我几乎完成了我的代码,但我需要帮助定时器进行冒泡排序和基数排序。时间总是零,我尝试了一切,但它总是最终为零。这是代码有问题吗?或我正在使用的计时器类型。

更新...所以我现在修复时间我正在进行排序测试,以确定该函数是否对数组进行了排序......这就是我所拥有的,它仍然是不确定的。打印时,它会开始显示多个“未排序”和“已排序”。

#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 个答案:

答案 0 :(得分:0)

time没有足够好的解决方案来完成任务。请改用clock,如下所示:

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

你也可能想要多次运行例程(例如100或1000),然后将总时间除以例程的重复次数。