Back

Array Operations Visualization

Interactive Demonstration

Visualize common array operations: Access, Insertion, and Deletion.

O(1)
O(n)
O(n)

Enter an initial array or perform an operation.

How Array Operations Work

Example Implementation (Conceptual)

class ArrayWrapper {
    constructor() {
        this.array = [];
    }

    access(index) {
        if (index < 0 || index >= this.array.length) {
            throw new Error("Index out of bounds");
        }
        // Direct access: O(1)
        return this.array[index];
    }

    insert(index, value) {
        if (index < 0 || index > this.array.length) { // Allow inserting at the end
            throw new Error("Index out of bounds");
        }
        // Shift elements: O(n)
        this.array.splice(index, 0, value);
    }

    delete(index) {
        if (index < 0 || index >= this.array.length) {
            throw new Error("Index out of bounds");
        }
        // Shift elements: O(n)
        this.array.splice(index, 1);
    }
}