在C++中使用冒泡排序时出现意外值
Unexpected Value While using Bubble Sort in C++
当使用气泡排序方法从最小到最大对数组进行排序时,它输出的意外值是:-858993460。
在调试器中,我被提示"围绕变量'numb 已损坏'的堆栈。
我目前正在使用Visual Studios来运行代码。
我还在新项目中运行了相同的代码,但没有结果。
#include <iostream>
int main()
{
int length = 6;
int temp = 0;
int end = 6;
int numb[] = { 6, 5, 4, 3, 2, 1 };
for (int counter = length - 1; counter > 0; counter--)
{
for (int i = 0; i < end; i++)
{
if (numb[i] > numb[i + 1])
{
temp = numb[i + 1];
numb[i + 1] = numb[i];
numb[i] = temp;
}
}
for (int i = 0; i <= 5; i++)
{
std::cout << numb[i] << " ";
}
std::cout << "n";
end--;
}
system("pause");
}
在你内部for
循环中int i = 0; i < end; i++
,你需要将条件设置为i < end - 1
。这是因为在交换索引时,您已经与i + 1
一起位于数组的末尾。
相关文章:
- 如何在C++中对数组进行冒泡排序
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 在C++中使用冒泡排序时出现意外值
- 冒泡排序中所需的交换次数,而无需实际对其进行排序
- 如何对文件中数组中的单个列进行冒泡排序?C++
- 需要关于如何将我的随机数数组连接到冒泡排序的帮助
- 冒泡排序无法对C++中的对象的动态数组进行排序
- 冒泡排序.C++
- 链表的冒泡排序
- 有没有选择排序优于冒泡排序的输入
- C++:如何使用冒泡排序来重新排列我的数据
- 冒泡排序随机数
- 矢量下标超出范围-冒泡排序
- 使用冒泡排序比较类对象
- 在c++中使用冒泡排序/交换对字符串进行排序
- 矢量下标超出范围错误,C++,冒泡排序
- 冒泡排序、选择排序和插入排序
- 使用冒泡排序c++对结构数组的成员进行优先级排序
- 冒泡排序链表C++
- 行搜索和冒泡排序的问题