我遇到了使用以下代码对1000个整数数组进行排序的问题。它与10个整数一起工作正常但是1000似乎继续进行我假设它是某种内存泄漏。
在我的错误的一个小解释之前,我从来没有排序过。
谢谢
class RandomNumbers
{
private:
int intRandomNumbers;
public:
int getRandomNumbers();
void setRandomNumbers(int intPRandomNumbers);
RandomNumbers(void);
};
-----------------------------------------------------------
void printArray(const int intArray[], int intLength);
int searchFull(const int intArray[], int intLength, int intSearchItem);
void sortBubble(int intArray[], int intLength);
int searchLinear(const int intArray[], int intLength, int intSearchItem);
int searchBinary(const int intArray[], int intLength, int intSearchItem);
--------
//Getters and setters for RandomNumber class.
RandomNumbers::RandomNumbers()
{
setRandomNumbers(0);
}
void RandomNumbers::setRandomNumbers(int intPRandomNumbers)
{
intRandomNumbers = intPRandomNumbers;
}
int RandomNumbers::getRandomNumbers()
{
return intRandomNumbers;
}
void printArray(const int intArray[], int intLength) {
for(int intIndex = 0; intIndex < intLength - 1; intIndex++) {
cout << intArray[intIndex] << ", ";
}
cout << intArray[intLength - 1] << endl;
}
int searchFull(const int intArray[], int intLength, int intSearchItem) {
int intLocation = -1;
int intCounter = 1;
for(int intIndex = 0; intIndex < intLength; intIndex++) {
cerr << "searchFull: " << intCounter++ << endl;
if(intArray[intIndex] == intSearchItem)
{
intLocation = intIndex;
}
}
return intLocation;
}
void sortBubble(int intArray[], int intLength){
int intTemp = 0;
int intIteration = 0;
int intIndex = 0;
for(intIteration = 1; intIteration < intLength; intIteration++) {
for(intIndex = 0; intIndex < intLength - intIteration; intIndex++) {
if(intArray[intIndex] > intArray[intIndex + 1]) {
intTemp = intArray[intIndex];
intArray[intIndex] = intArray[intIndex + 1];
intArray[intIndex + 1] = intTemp;
}
printArray(intArray,intLength);
}
}
}
int searchLinear(const int intArray[], int intLength, int intSearchItem) {
int intLocation = -1;
int intCounter = 1;
for(int intIndex = 0; intIndex < intLength && intSearchItem >= intArray[intIndex]; intIndex++) intIndex++)
{
cerr << "searchLinear: " << intCounter++ << endl;
if(intArray[intIndex] == intSearchItem)
{
intLocation = intIndex;
}
}
return intLocation;
}
int searchBinary(const int intArray[], int intLength, int intSearchItem) {
int intFirstIndex = 0;
int intLastIndex = intLength - 1;
int intMiddle = 0;
bool boolFound = false;
while(intFirstIndex <= intLastIndex && !boolFound) {
intMiddle = (intFirstIndex + intLastIndex) / 2;
if(intArray[intMiddle] == intSearchItem) {
boolFound = true;
} else if(intArray[intMiddle] > intSearchItem) {
intLastIndex = intMiddle - 1;
} else {
intFirstIndex = intMiddle + 1;
}
cerr << "searchBinary: " << intFirstIndex << ", " << intMiddle << ", " << intLastIndex << endl;
}
if(boolFound) {
return intMiddle;
} else {
return -1;
}
}
int main() {
srand (time(NULL));
ofstream myfile;
myfile.open ("RandomNumber.txt");
if (myfile.is_open())
{
for (int i = 0; i < 1000; ++i)
{
int RandomNumbers = rand() % 1000 + 1;
myfile << RandomNumbers << "\n";
}
}
myfile.close();
int array_size = 1000;
int * array = new int[array_size];
int position = 0;
ifstream fin("RandomNumber.txt");
if(fin.is_open())
{
cout << "File opened!!! Loading array. ";
while(!fin.eof() && position < array_size)
{
fin >> array[position];
position++;
}
cout << "Displaying array..." << endl <<endl;
for(int intIndex = 0; intIndex < array_size; intIndex++)
{
cout << array[intIndex] << endl;
}
fin.close();
}
else
{
cout<< "File could not be opened." << endl;
}
cout << searchFull(array, array_size, 43) << endl;
cout << searchLinear(array, array_size, 43) << endl; //Incorrect not sorted
cout << searchFull(array, array_size, 5) << endl;
cout << searchLinear(array, array_size, 5) << endl; //Incorrect not sorted
sortBubble(array, array_size);
cout << searchFull(array, array_size, 43) << endl;
cout << searchLinear(array, array_size, 43) << endl;
cout << searchBinary(array, array_size, 43) << endl;
cout << searchFull(array, array_size, 5) << endl;
cout << searchLinear(array, array_size, 5) << endl;
cout << searchBinary(array, array_size, 5) << endl;
system("PAUSE");
return 0;
};
答案 0 :(得分:1)
while(!fin.eof()
是一个大错。在读取最后一个值之后但在eof()
发生之前会发生什么? (回答:你会把最后一个条目复制两次)。通过以下方式修复它:
while ( position < array_size && fin >> array[position] )
position++;
此外,您应该使用position
作为计数器进行显示和排序,而不是array_size
。
也许你的sortBubble
函数有错误,或者可能只需要很长时间才能运行。如果您尝试100个号码会发生什么?你可以为它发布代码吗?