C++ 泡沫及;基数排序试验

C++ 泡沫及;基数排序试验,c++,arrays,timer,bubble-sort,radix-sort,C++,Arrays,Timer,Bubble Sort,Radix Sort,我几乎完成了我的代码,但是我需要关于冒泡排序和基数排序计时器的帮助。时间总是为零,我尝试了所有方法,但结果总是为零。代码有问题吗。或者我正在使用的计时器类型 更新。。。所以我确定了现在我正在进行排序测试的时间,以确定函数是否对数组进行了排序。。。这就是我所拥有的,它仍然不确定。打印时,它开始显示多个“未排序”和“已排序” #include <iostream> #include <cstdlib> #include <ctime> using std::co

我几乎完成了我的代码,但是我需要关于冒泡排序和基数排序计时器的帮助。时间总是为零,我尝试了所有方法,但结果总是为零。代码有问题吗。或者我正在使用的计时器类型

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

#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 );
}
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::endl;
int const temp=10000;
void bubbleSort(int数组[temp]);
void radixSort(int数组[temp]);
int main()
{
int A1;
int数组[temp];
字符a='\0';
cout>a;
如果(a==“y”| | a==“y”)
{
srand((未签名)时间(0));
对于(尺寸i=0;icout
时间
对于任务没有足够好的分辨率。请使用
时钟
代替,如下所示:

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

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

temp
对于数组的
大小来说是一个非常糟糕的名字。
time\u t
(返回值
time
)通常分辨率为1秒。因此,如果运行时间少于1秒,则计时为零。您应该使用
clock
函数。不要中途更改问题。提出新问题。