C语言编程中的插入排序
在插入排序中,
- 我们如何将一个新整数插入到一个整数数组中?
- 数组的内存会在编译时分配,所以我们不能增加数组的大小,即使我们分配了一些额外的空间,如果所需的内存超过了分配的内存怎么办?
- 我们应该创建一个新数组来插入每个整数吗?
- 如果我们想在已排序的数组中插入更多数量的整数,该怎么办?
- 我们可以用指针来做到这一点吗?
In insertion sort,
- how can we insert a new integer into an array of integers?
- The memory for the array will be allocated during compilation,so we cannot increase the size of array,Even if we allocate some extra space,what should we do if the required memory exceeds the allocated memory?
- should we create a new array for the insertion of each and every integer?
- What should we do if we want insert more number of integers into the sorted array?
- Can we do this with pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您提出的所有问题都可以通过以下方式来回答:
在不止一种意义上,保存数组的内存是在编译期间分配的,这是不正确的。
在实际运行程序之前无法分配内存。
虽然数组的大小通常在编译时决定(例如
int array[32]
),但指针的大小却并非如此。例如,
int *array = malloc(many * sizeof(int));
为many
整数腾出空间。虽然不能增加为数组分配的内存量是正确的,但对于指针来说却不能增加分配的内存量。
例如,
array = realloc(many_more * sizeof(int));
为many_more
整数腾出空间。我建议您阅读本关于指针和数组的教程。
I think all the questions you asked can be answered by the following:
It is not true - in more than one sense - that the memory that holds arrays is allocated during compilation.
The memory cannot be allocated before you actually run the program.
While the size of an array is usually decided at compilation (e. g.
int array[32]
), the same is not true for pointers.For example,
int *array = malloc(many * sizeof(int));
makes room formany
integers.While it is true that you cannot increase the amount of memory allocated for an array, the same does not hold for pointers.
For example,
array = realloc(many_more * sizeof(int));
makes room formany_more
integers.I suggest you read this tutorial on pointers and arrays.