2.1Array Operations
Inserting at the end
The easiest place to add an element to an array is the end. The filled part starts at cell 0 and runs without gaps, so the first empty cell is known: size. The new value is written there and size goes up by one. No element moves.
1Idea
In an array holding size elements, the filled cells run from 0 to size - 1. The next cell, arr[size], is the first empty one. Writing the new value there and increasing size by one is all it takes. However long the array is, the work is two steps.
The only limit is the capacity. If size equals the capacity, arr[size] lies outside the array. C does not stop that write; the value lands in the memory after the array, on top of some other variable. So the function first checks size == CAPACITY and, if the array is full, returns false without touching it.
size is 8 and the capacity is 8. There is no cell arr[8], so the insertion is refused.In Java and Python the result comes back as the new size: size = insertAtEnd(arr, size, 9). Writing out of bounds raises ArrayIndexOutOfBoundsException in Java and IndexError in Python. The check is still made, because a full array is not an error but a normal situation.
2Step by step
The drawing adds 9 to the array holding 12, 7 and 3. The capacity is 8 and size is 3. At each step the matching line of code lights up.
- Start. The filled cells are 0, 1 and 2.
sizeis 3 andvalueis 9. sizeis 3 and the capacity is 8: the array is not full. The first empty cell isarr[3].arr[size] = value: 9 is written into cell 3. No other cell was touched.sizebecomes 4. The next insertion will go into cell 4.
#define CAPACITY 8 bool insertAtEnd(int arr[], int* size, int value) { if (*size == CAPACITY) // the array is full return false; arr[*size] = value; // the first empty cell (*size)++; return true;}static final int CAPACITY = 8; int insertAtEnd(int[] arr, int size, int value) { if (size == arr.length) // the array is full return size; arr[size] = value; // the first empty cell size++; return size;}CAPACITY = 8 def insert_at_end(arr, size, value): if size == len(arr): # the array is full return size arr[size] = value # the first empty cell size += 1 return sizeThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
One comparison, one write and one increment. It does not depend on the length of the array.
No extra memory is used.
In a singly linked list, inserting at the end takes O(n), because you walk from the head to the last node (inserting at the end of a linked list). In an array the end is computed from size.
4Common mistake
A common mistake is skipping the capacity check:
void insertAtEnd(int arr[], int* size, int value) { arr[*size] = value; // outside the array if size is 8 (*size)++;}When the array is full, this function writes to arr[8]. C does not stop it: the memory right after the array is corrupted, and the bug usually shows up somewhere else entirely. Every operation that writes into an array should check for room first.
5Try it
If you call insertAtEnd on an array whose size is 5, which cell gets the value?
The answer is B. The filled cells run from 0 to 4. The first empty cell is arr[5]: its index is size itself.
Show the answer
The answer is B. The filled cells run from 0 to 4. The first empty cell is arr[5]: its index is size itself.
What does insertAtEnd do when the array is full?
The answer is B. A fixed-size array never changes its capacity. The function sees size == CAPACITY and returns false without touching the array. Growing an array is covered in growing the capacity.
Show the answer
The answer is B. A fixed-size array never changes its capacity. The function sees size == CAPACITY and returns false without touching the array. Growing an array is covered in growing the capacity.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.