插入排序算法在放入函数时的行为不同
Insertion sort algorithm behaves differently when put in a function
我有以下代码来实现插入排序:
int main()
{
const int SIZE = 10;
int a[SIZE] = {8, 6, 10, 2, 16, 4, 18, 14, 12, 10};
//insertionSort(a, SIZE);
// The following code is used in the insertionSort() function call above. To use the function, uncomment the function call above, and comment out the code below
// Start of code used by insertionSort() function
int temp;
for (int i=1; i < SIZE; i++)
{
for (int j=i; a[j] < a[j-1]; j--)
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
// End of code used by insertionSort() function
return 0;
}
void insertionSort(int a[], const int SIZE)
{
int temp;
for (int i=1; i < SIZE; i++)
{
for (int j=i; a[j] < a[j-1]; j--)
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
当代码在main((中使用时,该算法工作正常,但是当通过insertionSort((调用它时,它会产生错误的结果。
我已经确定问题是这样的:当 j 达到 0 时,内部循环条件(a[j] <a[j-1](不会停止循环,因此它继续使用负索引访问数组。将条件更改为> 0 && a[j]
我想知道为什么这种行为仅在循环在 insertionSort(( 中运行时表现出来,而不是在 main(( 中运行时。
此循环
for (int j=i; a[j] < a[j-1]; j--)
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
调用未定义的行为,因为没有检查j
应大于 0
。
否则,例如,当j
等于 0
时,可以使用索引-1
访问数组之外
a[0] < a[-1]
这是一个带有更新循环的演示程序
#include <iostream>
#include <utility>
int main()
{
int a[] = { 8, 6, 10, 2, 16, 4, 18, 14, 12, 10 };
const size_t SIZE = sizeof( a ) / sizeof( *a );
for ( const auto &item : a ) std::cout << item << ' ';
std::cout << 'n';
for ( size_t i = 1; i < SIZE; i++ )
{
for ( size_t j = i; j != 0 && a[j] < a[j-1]; --j )
{
std::swap( a[j-1], a[j] );
}
}
for ( const auto &item : a ) std::cout << item << ' ';
std::cout << 'n';
return 0;
}
它的输出是
8 6 10 2 16 4 18 14 12 10
2 4 6 8 10 10 12 14 16 18
并将算法包含在函数中
#include <iostream>
#include <utility>
void insertionSort( int a[], size_t n )
{
for ( size_t i = 1; i < n; i++ )
{
for ( size_t j = i; j != 0 && a[j] < a[j-1]; --j )
{
std::swap( a[j-1], a[j] );
}
}
}
int main()
{
int a[] = { 8, 6, 10, 2, 16, 4, 18, 14, 12, 10 };
const size_t SIZE = sizeof( a ) / sizeof( *a );
for ( const auto &item : a ) std::cout << item << ' ';
std::cout << 'n';
insertionSort( a, SIZE );
for ( const auto &item : a ) std::cout << item << ' ';
std::cout << 'n';
return 0;
}
输出与上图相同
8 6 10 2 16 4 18 14 12 10
2 4 6 8 10 10 12 14 16 18
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- STL算法函数在多个一维容器上的使用
- A*算法中魔方的启发式函数 人工智能
- 使用指针算法修改函数中的 2D 数组
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 如何在算法中使用谓词函数find_if?
- 我们如何并行运行算法的 n 个实例并以有效的方式计算结果函数的平均值?
- 如何在复杂的算法中处理goto函数?
- 快速排序函数在快速排序算法中如何工作?
- 在 c++ 或 python 中生成一个体面的视差图以在 Raspberry Pi 上实现的最佳方法(算法或函数)是什么
- 阴算法(音高检测) - 差分函数的替代
- 算法中的排序函数返回错误
- 如何处理算法中具有不同名称成员函数的类?
- 在 stl 算法中使用函数对象
- 插入排序算法在放入函数时的行为不同
- std::bitset 哈希函数算法
- 轻量级 8 字节哈希函数算法
- C++中的合并函数算法有问题
- C++有序链表搜索函数算法逻辑
- C++sort()函数算法