algoritmauzmanı
Sign in
TREN
2.1Array Operations
Lesson 2.1.5

Inserting at the front

Inserting at the front is inserting at a position with position 0. It is the most costly insertion in an array: if the new element goes into cell 0, every existing element has to move one cell to the right. In a linked list the same operation is a single step.

What you will learn
  • That inserting at the front is a special case of inserting at a position
  • Why every element moves and why the operation takes time
  • Why the same operation is in a linked list
5 minTime O(n)Memory O(1)C · Java · Python

1Idea

The only way to free cell 0 of an array is to move the element there, and every element after it, one place right. The loop starts with i = size, writes arr[i] = arr[i - 1] while i > 0 and counts down. It is the same loop as in inserting at a position; only pos has been replaced by 0.

When the loop ends, the value in arr[0] is only a copy, and the new value is written over it. Then size goes up by one. If the array is full there is no room to shift into; the function returns false and nothing changes. In an empty array the loop never runs and the value goes straight into cell 0.

So an array is a poor choice for a program that keeps adding elements at the front. Adding n elements takes 1 + 2 + ... + n shifts, which is work. Inserting at the head of a linked list moves no element at all.

2Step by step

The drawing inserts 5 at the front of the array holding 12, 7 and 3. The capacity is 8 and size is 3. The result will be 5, 12, 7, 3. A faded cell holds a value that has already been copied and is about to be overwritten.

012345671273537123size4size5valuei
  1. Start. size is 3 and the capacity is 8: there is room. The new value will go into cell 0.
  2. i = 3: arr[3] = arr[2]. 3 is copied into the empty cell.
  3. i = 2: arr[2] = arr[1]. 7 moves one place right, over the old copy of 3.
  4. i = 1: arr[1] = arr[0]. 12 moves one place right. Every element has shifted; when i reaches 0 the loop ends.
  5. arr[0] = value: 5 is written into cell 0.
  6. size becomes 4. Result: 5, 12, 7, 3. All three elements moved.

#define CAPACITY 8 bool insertAtFront(int arr[], int* size, int value) {    if (*size == CAPACITY)                // the array is full        return false;    for (int i = *size; i > 0; i--)       // everyone moves right        arr[i] = arr[i - 1];    arr[0] = value;                       // the first cell is free    (*size)++;    return true;}
static final int CAPACITY = 8; int insertAtFront(int[] arr, int size, int value) {    if (size == arr.length)               // the array is full        return size;    for (int i = size; i > 0; i--)        // everyone moves right        arr[i] = arr[i - 1];    arr[0] = value;                       // the first cell is free    size++;    return size;}
CAPACITY = 8 def insert_at_front(arr, size, value):    if size == len(arr):  # the array is full        return size    for i in range(size, 0, -1):  # everyone moves right        arr[i] = arr[i - 1]    arr[0] = value  # the first cell is free    size += 1    return size

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(n)

The loop runs size times: every one of the n elements moves one cell.

Memory
O(1)

Only the loop counter is used; the array changes in place.

In a linked list, inserting at the head is : the new node is linked to the old first node and is moved to the new node. An array takes O(1) at the end and O(n) at the front. Which end gets most insertions decides which structure to use.

4Common mistake

A common mistake is starting the loop at size - 1:

for (int i = *size - 1; i > 0; i--)    arr[i] = arr[i - 1];   // the last element never moves

This loop does not move the last element into the empty cell; it writes the one before it over the last element instead. After the loop the array 12, 7, 3 becomes 12, 12, 7 and 3 is lost. When shifting right, the first cell written is the first empty cell, arr[size].

5Try it

If you add 1, 2 and 3 in that order to an empty array with insertAtFront, what does the array hold?

Show the answer

The answer is B. Each new value goes into cell 0 and pushes the others one place right. First 1, then 2, 1, and finally 3, 2, 1. Inserting at the front reverses the order.

How many assignments does inserting at the front of a 1000-element array take?

Show the answer

The answer is B. The loop runs from i = 1000 down to 1 and each of the 1000 elements moves one cell. Then the new value is written into arr[0]. That is work.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.