2.1Array Operations
Inserting at a position
When inserting at the end, the new element went into the first empty cell. When inserting at a position, the new element goes into the middle of the array, for example at index 1. That cell is taken, so room is made first: every element from that position on moves one cell to the right.
- That valid positions run from 0 to
size, and why nothing is inserted into a full array - Why the shifting runs from the end towards the front
- Why the operation takes O(n) time and how it differs from a linked list
1Idea
In an array the elements sit in contiguous memory and there can be no gaps. To put the new value into cell pos, the element in that cell and every element after it move one cell to the right. When the move is done, arr[pos] is free, the value is written there and size goes up by one.
Two checks come first. If the array is full (size == CAPACITY), there is no room to shift into; the function returns false and the array does not change. If the position is less than 0 or greater than size, nothing is inserted either. pos == size is valid: no element has to move and the operation is inserting at the end. pos == 0 is inserting at the front.
The shifting runs from the end towards the front. The loop starts with i = size and writes arr[i] = arr[i - 1] on each pass: first the last element moves into the empty cell, then the one before it moves into its old place. Every element is moved before anything is written over it.
The insert method of a Python list does the same shifting. It is one line of code, but its time still depends on how many elements move.
2Step by step
The drawing inserts 5 at position 1 of the array holding 12, 7, 3 and 9. The capacity is 8 and size is 4. The result will be 12, 5, 7, 3, 9. A faded cell holds a value that has already been copied and is about to be overwritten.
- Start.
sizeis 4 and the capacity is 8: there is room.posis 1, which lies between 0 and 4: valid. i = 4:arr[4] = arr[3]. 9 is copied into the empty cell. Cell 3 still holds 9 too, but the next step writes over it.i = 3:arr[3] = arr[2]. 3 moves one place right, over the old copy of 9.i = 2:arr[2] = arr[1]. 7 moves one place right. Now 7, 3 and 9 have all shifted.i = 1: the conditioni > posfails and the loop ends. The value in cell 1 is only a copy now: the new value can go here.arr[pos] = value: 5 is written into cell 1.sizebecomes 5. Result: 12, 5, 7, 3, 9. The number of elements moved issize - pos, which is 3.
#define CAPACITY 8 bool insertAt(int arr[], int* size, int pos, int value) { if (*size == CAPACITY) // the array is full return false; if (pos < 0 || pos > *size) // invalid position return false; for (int i = *size; i > pos; i--) // from the end towards the front arr[i] = arr[i - 1]; // shift one place right arr[pos] = value; (*size)++; return true;}static final int CAPACITY = 8; int insertAt(int[] arr, int size, int pos, int value) { if (size == arr.length) // the array is full return size; if (pos < 0 || pos > size) // invalid position return size; for (int i = size; i > pos; i--) // from the end towards the front arr[i] = arr[i - 1]; // shift one place right arr[pos] = value; size++; return size;}CAPACITY = 8 def insert_at(arr, size, pos, value): if size == len(arr): # the array is full return size if pos < 0 or pos > size: # invalid position return size for i in range(size, pos, -1): # from the end towards the front arr[i] = arr[i - 1] # shift one place right arr[pos] = value size += 1 return sizeThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
The loop runs size - pos times. In the worst case, position 0, all n elements move.
Only the loop counter is used; the array changes in place.
Near the end few elements move, and at pos == size none do, so the operation is O(1). A linked list is the other way round: linking takes O(1), walking to the position takes O(n) (inserting at a position in a linked list).
4Common mistake
The most common mistake is shifting from the front towards the end:
for (int i = pos; i < *size; i++) arr[i + 1] = arr[i]; // 7 is written over 3 firstIn the array 12, 7, 3, 9 with position 1, the first pass writes arr[2] = 7 and 3 is lost. The next passes carry the same 7 forward: when the loop ends the array is 12, 7, 7, 7, 7, and 3 and 9 are gone. When shifting right, always start at the end. Deleting from the middle moves elements left, and there the loop runs from the front towards the end.
5Try it
What happens if you call insertAt(arr, &size, 4, 8) on an array whose size is 4?
The answer is B. pos == size is a valid position. The loop starts with i = 4 and i > 4 is false at once. The value is written into cell 4, which is the same as inserting at the end. Position 5 would be invalid.
Show the answer
The answer is B. pos == size is a valid position. The loop starts with i = 4 and i > 4 is false at once. The value is written into cell 4, which is the same as inserting at the end. Position 5 would be invalid.
What happens if you try to insert 1000 times into an array with capacity 8?
The answer is B. A fixed-size array never changes its capacity. When it is full there is no room to shift into, so the function returns false without changing anything. Growing the capacity is the job of dynamic arrays.
Show the answer
The answer is B. A fixed-size array never changes its capacity. When it is full there is no room to shift into, so the function returns false without changing anything. Growing the capacity is the job of dynamic arrays.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.