Arrays 长度为1M的排序数组的数组声明中可能存在错误

Arrays 长度为1M的排序数组的数组声明中可能存在错误,arrays,c,sorting,Arrays,C,Sorting,我需要使用选择排序对100K、150K、200K和1M长度的数组进行排序,并显示排序每个数组所需的时间以及元素之间比较的数量 为此,我制作了一个数组,数组中的元素作为数组的长度。然后,我使用所述数组运行for循环,并传递用作要创建的未排序数组大小的元素,然后创建一个随机生成的数组 然后我使用函数调用进行排序 该程序在我的前3个数组长度(100K、150K和200K)中运行时没有问题,但在1M数组长度排序中根本没有运行 我无法确定程序在哪一点停止执行。我首先认为问题在于我的整数声明,因为其中一些声

我需要使用选择排序对100K、150K、200K和1M长度的数组进行排序,并显示排序每个数组所需的时间以及元素之间比较的数量

为此,我制作了一个数组,数组中的元素作为数组的长度。然后,我使用所述数组运行for循环,并传递用作要创建的未排序数组大小的元素,然后创建一个随机生成的数组

然后我使用函数调用进行排序

该程序在我的前3个数组长度(100K、150K和200K)中运行时没有问题,但在1M数组长度排序中根本没有运行

我无法确定程序在哪一点停止执行。我首先认为问题在于我的整数声明,因为其中一些声明存储的数量高达一百万。为此,我周期性地放置printf语句,以了解程序流在哪里被中断,令人惊讶的是数组声明(在我看来,这是一个样子,但不确定)

我已经附上了我得到的代码和输出

代码:


由于其他一切都在运行,我想说您没有足够的位来在运行中分配1M大小的连续数组,因此正如其他人所评论的,我建议
malloc()

你班上的其他人可能会这样做,因为他们的机器上有足够的堆栈空间,可以在移动中分配1M。查看
malloc()
是否解决了这个问题,然后您就可以确定了。

在任何函数之外定义您的大数组

int large[1000000];
int main(void) {
    int sz[4] = {100000, 150000, 200000, 1000000};
    for (int k = 0; k < 4; k++) {
        // use the first sz[k] elements of large
    }
}
int大型[1000000];
内部主(空){
int sz[4]={100000,150000,2000000,1000000};
对于(int k=0;k<4;k++){
//使用大的第一个sz[k]元素
}
}

对于堆栈来说,它可能太大了。考虑在堆上分配(使用<代码> MalCube()/代码>)。我也这样想,但是,解决方案需要用数组来完成,因为我们还没有处理堆。我的同事已经能够用数组解决上述问题。您的环境是什么?在shell上执行命令
ulimit-s unlimited
(删除对堆栈大小的限制)可能会有帮助。@ANIRUDHBUVANESH尝试可以,但没有帮助。更有希望的尝试是使用
char
而不是
int
作为数组的元素来节省内存。随机数最多为100,因此最多可存储127的带符号
char
就足够了-使用
malloc
1st check: It's the long long int declaration
2nd check: It's the array declaration
3rd check: It's not the array declaration
4th check: It's the random array generation
5th check: It's the function call
Selection Sort - time = 11.458000 seconds, element by element comparisons = 4999950000
1st check: It's the long long int declaration
2nd check: It's the array declaration
3rd check: It's not the array declaration
4th check: It's the random array generation
5th check: It's the function call
Selection Sort - time = 59.196000 seconds, element by element comparisons = 16249875000
1st check: It's the long long int declaration
2nd check: It's the array declaration
3rd check: It's not the array declaration
4th check: It's the random array generation
5th check: It's the function call
Selection Sort - time = 120.349000 seconds, element by element comparisons = 36249775000
1st check: It's the long long int declaration
2nd check: It's the array declaration
int large[1000000];
int main(void) {
    int sz[4] = {100000, 150000, 200000, 1000000};
    for (int k = 0; k < 4; k++) {
        // use the first sz[k] elements of large
    }
}