Array Operations


Arrays support a variety of operations that allow us to manipulate and process data efficiently. These operations include inserting new elements, deleting existing ones, updating values, and traversing the array. Below, we walk through each of these key operations with step-by-step visualizations.

1. Insertion

Insertion is the process of adding a new element to the array. If the array has available space, the element can be added at a specific index. However, this may require shifting elements to make space.

Initial Array:

{ "array": [10, 20, 30, 40, 50], "showIndices": true }

Insert 25 at index 2: Elements from index 2 onwards are shifted to the right to make space.

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [2, 3, 4] }
{ "array": [10, 20, 25, 30, 40, 50], "showIndices": true, "emptyIndices": [2], "highlightIndices": [3, 4, 5] }

Insert 25 at index 2:

{ "array": [10, 20, 25, 30, 40, 50], "showIndices": true, "highlightIndicesGreen": [2], "highlightIndices": [3, 4, 5] }

Resulting Array:

{ "array": [10, 20, 25, 30, 40, 50], "showIndices": true }

2. Deletion

Deletion removes an element from a specific index. After removal, all subsequent elements are shifted left to fill the gap.

Initial Array:

{ "array": [10, 20, 30, 40, 50], "showIndices": true }

Delete element at index 2 (value: 25):

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndicesRed": [2] }
{ "array": [10, 20, 30, 40, 50], "showIndices": true, "emptyIndices": [2] }

After shifting items on right side of index 2 (value: 25), to left, by one position:

{ "array": [10, 20, 40, 50], "showIndices": true, "highlightIndices": [2, 3] }

Resulting Array:

{ "array": [10, 20, 40, 50], "showIndices": true }

3. Traversal

Traversal involves visiting each element of the array in sequence. This is usually done with a loop and is fundamental to many other operations such as searching or updating.

Traverse from left to right:

index = 0

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [0] }

index = 1

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [1] }

index = 2

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [2] }

index = 3

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [3] }

index = 4

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [4] }

4. Update

Updating an element involves changing the value at a specific index. This is a direct operation and doesn’t require shifting.

Initial Array:

{ "array": [10, 20, 30, 40, 50], "showIndices": true }

Update value at index 3 (change 40 to 99):

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [3] }
{ "array": [10, 20, 30, 99, 50], "showIndices": true, "specialIndices": [3] }

After update:

{ "array": [10, 20, 30, 99, 50], "showIndices": true, "specialIndices": [3] }

Searching involves finding whether a value exists in the array and, if it does, returning its index. This is often done via linear or binary search depending on whether the array is sorted.

Search for value 30:

{ "array": [10, 20, 30, 99, 50], "showIndices": true, "highlightIndices": [2] }

Search for value 70 (not found):

{ "array": [10, 20, 30, 99, 50], "showIndices": true }

No match found in the array.