C++插入排序不适用于大型数组
C++ insertion sort not working for large arrays
my insertionSort 函数适用于小数组,但不适用于具有 50,000 个随机值的数组。我花了几个小时试图弄清楚这一点,但我被难住了。这是代码:
void insertionSort(int array[], int length) {
int swapHolder, counter, index;
for (counter = 1; counter < length; counter++) {
index = counter;
while (counter > 0 && array[index - 1] > array[index]) {
swapHolder = array[index];
array[index] = array[index - 1];
array[index - 1] = swapHolder;
index--;
}
}
}
我的另一个排序函数(bubbleSort)适用于大型数组,但我在这个问题上挂断了。
行
while (counter > 0 && array[index - 1] > array[index]) {
应该是
while (index > 0 && array[index - 1] > array[index]) {
更深层次地说,插入排序的平均复杂度O(n^2)
,因此最适合小型数组。也就是说,它不是对 50,000 个值进行排序的正确算法。
相关文章:
- 适用于大型数组的无复制线程安全环形缓冲区
- 为大型数组提供堆大小的编译器
- 快速排序不适用于大型数组
- 如何在C++中创建大型数组(如60亿)?
- 为什么我的代码无法处理大型数组输入(>10000)?
- 在能够从 Web 浏览器访问大型数组数据的同时存储它的最可行方法是什么?
- c++ 从大型数组中读取 3D 坐标并计算它们之间的距离
- 在C 中声明具有默认值的大型数组
- 提高访问大型数组元素的性能
- C++保存大型数组的最佳方法
- C++将大型数组拆分/分区为较小的数组
- 对象大型数组的内存使用量极大
- C++中使用大型数组时的性能问题
- C++ 数组大小以及如何管理大型数组
- std::string 或 char[] 作为大型数组状结构的元素
- 程序在大型数组初始化时终止
- 在C++中使用大型数组
- 使用大型数组索引增加访问时间
- 具有大型数组的 C++ 堆栈溢出未经处理的异常
- 处理大型数组