"bubble sort algorithm" 小错误
The "bubble sort algorithm" small error
我被分配到:
》编写一个函数,实现整数数组的冒泡排序,函数原型如下:void sort(int data[], int count);
编写一个程序,提示输入文件名,打开该文件并将文件中的数字读入数组。然后你的程序应该调用你的排序例程,然后打印结果数组。"
我完成了它,它工作得很好,直到我被告知如果数字已经排好,它也必须提前停止。您将在下面的 void 函数中看到该尝试。我没有收到任何错误;程序可以正常编译、执行和关闭。现在的问题是添加了布尔变量,它不再对数字进行排序。它将按照与文本文件相同的顺序打印出来。我通过调试器 运行 它并注意到 "ii" 变量并没有像它应该的那样每次增加 1,for(... ; ... ; ii++) ,而是停留在0. 有什么想法吗?
"integers.txt" 文件中的 (运行dom) 数字:12 42 5 67 41 9 19 93 10 124 21
void sort(int data[], int count);
int main()
{
const int MAX_SIZE = 128;
char fileName[MAX_SIZE];
int data[MAX_SIZE];
int count = 0;
cout << "Enter a file name: "; //integers.txt
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin.getline(fileName, sizeof(fileName));
ifstream input(fileName); //opens the file and reads the first line
if (input.is_open())
{
while (!input.eof() && count <= MAX_SIZE) //adds data to the array until the end of the file is reached
{
input >> data[count];
count += 1;
}
input.close();
}
else
{
cout << "\nThe file failed to open, try again.\n";
}
sort(data, count); //calls the bubble sort function
return 0;
}
void sort(int data[], int count)
{
int temp = 0;
int pass = 0;
bool sorted = false;
for (int pass = 0; pass <= count; pass += 1) //counts the number of passes
{
for (int ii = 0; (ii <= (count - pass - 1)) && (sorted = false) ; ii++) //sorts the integers from least to greatest
{ //also 'supposed to' stop early if already sorted
if (data[ii] > data[ii + 1])
{
sorted = false;
temp = data[ii];
data[ii] = data[ii + 1];
data[ii + 1] = temp;
}
}
}
cout << "\nSorted integers: ";
for (int jj = 1; jj <= count; jj += 1) //prints the sorted integers
{
cout << data[jj] << " ";
}
cout << "\n\n";
}
您想在外循环中进行 sorted
检查。即:
for (int pass = 0; pass < count && !sorted; pass += 1) //counts the number of passes
{
sorted = true;
for (int ii = 0; ii < (count - pass); ii++) //sorts the integers from least to greatest
{
这里的想法是,在每次遍历开始时,您假设数组已排序。如果进行交换,则数组可能未排序,因此将标志设置为 false
。仅当您在没有任何交换的情况下完成整个通过时,该标志才会为真。
此外,请注意我将您的外循环比较更改为 pass < count
而不是 pass <= count
。请记住,当您从 0 开始时,限制是 count-1
.
我还将内循环条件从<= (count - pass - 1)
更改为< (count - pass)
。它们是等价的,但后者更简洁。
我被分配到:
》编写一个函数,实现整数数组的冒泡排序,函数原型如下:void sort(int data[], int count);
编写一个程序,提示输入文件名,打开该文件并将文件中的数字读入数组。然后你的程序应该调用你的排序例程,然后打印结果数组。"
我完成了它,它工作得很好,直到我被告知如果数字已经排好,它也必须提前停止。您将在下面的 void 函数中看到该尝试。我没有收到任何错误;程序可以正常编译、执行和关闭。现在的问题是添加了布尔变量,它不再对数字进行排序。它将按照与文本文件相同的顺序打印出来。我通过调试器 运行 它并注意到 "ii" 变量并没有像它应该的那样每次增加 1,for(... ; ... ; ii++) ,而是停留在0. 有什么想法吗?
"integers.txt" 文件中的 (运行dom) 数字:12 42 5 67 41 9 19 93 10 124 21
void sort(int data[], int count);
int main()
{
const int MAX_SIZE = 128;
char fileName[MAX_SIZE];
int data[MAX_SIZE];
int count = 0;
cout << "Enter a file name: "; //integers.txt
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin.getline(fileName, sizeof(fileName));
ifstream input(fileName); //opens the file and reads the first line
if (input.is_open())
{
while (!input.eof() && count <= MAX_SIZE) //adds data to the array until the end of the file is reached
{
input >> data[count];
count += 1;
}
input.close();
}
else
{
cout << "\nThe file failed to open, try again.\n";
}
sort(data, count); //calls the bubble sort function
return 0;
}
void sort(int data[], int count)
{
int temp = 0;
int pass = 0;
bool sorted = false;
for (int pass = 0; pass <= count; pass += 1) //counts the number of passes
{
for (int ii = 0; (ii <= (count - pass - 1)) && (sorted = false) ; ii++) //sorts the integers from least to greatest
{ //also 'supposed to' stop early if already sorted
if (data[ii] > data[ii + 1])
{
sorted = false;
temp = data[ii];
data[ii] = data[ii + 1];
data[ii + 1] = temp;
}
}
}
cout << "\nSorted integers: ";
for (int jj = 1; jj <= count; jj += 1) //prints the sorted integers
{
cout << data[jj] << " ";
}
cout << "\n\n";
}
您想在外循环中进行 sorted
检查。即:
for (int pass = 0; pass < count && !sorted; pass += 1) //counts the number of passes
{
sorted = true;
for (int ii = 0; ii < (count - pass); ii++) //sorts the integers from least to greatest
{
这里的想法是,在每次遍历开始时,您假设数组已排序。如果进行交换,则数组可能未排序,因此将标志设置为 false
。仅当您在没有任何交换的情况下完成整个通过时,该标志才会为真。
此外,请注意我将您的外循环比较更改为 pass < count
而不是 pass <= count
。请记住,当您从 0 开始时,限制是 count-1
.
我还将内循环条件从<= (count - pass - 1)
更改为< (count - pass)
。它们是等价的,但后者更简洁。