Question

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?
Was it helpful?

Solution

I think all the questions you asked can be answered by the following:

  1. It is not true - in more than one sense - that the memory that holds arrays is allocated during compilation.

    1. The memory cannot be allocated before you actually run the program.

    2. 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 for many integers.

  2. 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 for many_more integers.

I suggest you read this tutorial on pointers and arrays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top