【发布时间】:2015-08-25 15:02:44
【问题描述】:
我一直在为一个项目开发这个程序。一切运行良好,但我的冒泡排序有问题。我运行并显示了函数的结果,但它经常显示负数,这是不应该的。而且每隔一段时间它就不能正确排序,这意味着我的冒泡排序功能没有按顺序对它们进行排序。
#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, int arraySize );
void display ( int btime);
int main ( )
{
int A1;
int array [ temp ];
char a = '\0';
cout << "\nWould You Like To Perform Bubble and Radix Test? (y/n): ";
cin >> a;
while ( a == 'y' || a == 'Y' )
{
srand ( ( unsigned ) time ( 0 ) );
for ( size_t i = 0; i < temp; i++ )
{
A1 = ( rand ( ) % temp ) + 1 ;
array [ i ] = A1;
bubbleSort ( array );
}
}
while ( a == 'n' || a == 'N' )
{
break;
}
return 0;
}
void bubbleSort ( int array [ temp ] )
{
for ( int i = 0; i < temp; i++ )
{
for ( int j = 0; j < temp - 1; j++ )
{
if ( array [ j ] > array [ j + 1 ] )
{
int temp = array [ j ];
array [ j ] = array [ j + 1 ];
array [ j + 1 ] = temp;
//Test: to see if its working properly.
cout << array [ j + 1 ] << endl; //idk if this should be placed here
}
}
}
}
【问题讨论】:
-
在 for() 循环中,将一个整数添加到未初始化的数组中,然后运行 bubbleSort(array)。
-
由于问题与基数排序无关,您可能根本不应该在问题中提及。
标签: c++ arrays sorting bubble-sort