c++ - 在数组c++上调用排序函数

标签 c++ arrays sorting

我正在使用插入排序函数对 5000 int 升序数组进行排序。当我将数组的地址传递给函数调用时,我得到 [Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]。也许我应该使用 vector ,但我不熟悉它们。也许我编码有误?

#include <iostream>
#define SIZE 5000 //array size
using namespace std;

void insertionSort(int arr[], int length) {
      int i, j, tmp;
      for (i = 1; i < length; i++) {
            j = i;
            while (j > 0 && arr[j - 1] > arr[j]) {
                  tmp = arr[j];
                  arr[j] = arr[j - 1];
                  arr[j - 1] = tmp;
                  j--;
            }
      }
}

int main() {
    int a[SIZE];
    int count = 1;

    for(int i=0; i<SIZE; i++) {
        a[i] = count;
        count++;
    }

    for(int i = 0; i < SIZE; i++) {
        printf("%d\t", a[i]);
    }

    insertionSort(&a[i], SIZE);
}

最佳答案

你可能想打电话

 insertionSort(a, SIZE)

关于c++ - 在数组c++上调用排序函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30042069/

相关文章:

c++ - 使用变量索引对 vector 的 vector 进行排序

c++ - C++ 中的多态性(意外行为)

c++ - 如何释放数组的单个元素?

javascript - 根据相同名称隔离数组

c++ - 将逗号分隔的十六进制(字符串)数组转换为整数或 float

c - 如何增加C中数组的大小

C++ 模板 : problem with member specialization

c++ - 包裹在 rice/ruby 中的纯虚拟 c++ 类在运行时引发 TypeError ("is not a class (Module)")

python - 在包含格式为 ('hour' 、 'min' 、 'AM/PM' 的时间元组的列表中查找时间的最大值

java - 对 int 数组进行降序排序