Index-based access
An array address can be calculated from its base address, element size, and index.
This is why reading array[i] takes O(1) time.
An array stores elements in an indexed sequence. In the classical fixed-size array model, elements occupy contiguous memory locations, enabling direct access by index.
Create an array or select an operation.
An array address can be calculated from its base address, element size, and index.
This is why reading array[i] takes O(1) time.
Inserting or deleting in the middle requires shifting later elements. The simulator visualizes this behavior even though JavaScript arrays are dynamic.
Linear search checks elements from left to right until it finds the target or reaches the end. Its worst-case running time is O(n).
Bubble Sort repeatedly compares adjacent values and swaps them when they are out of order. The optimized version stops early when a complete pass performs no swaps.
| Operation | Typical time | Reason |
|---|---|---|
| Access by index | O(1) | The address is calculated directly. |
| Set by index | O(1) | The target position is known. |
| Linear search | O(n) | Every element may need to be checked. |
| Insert/delete in the middle | O(n) | Later elements may need to shift. |
| Reverse | O(n) | Pairs are swapped from both ends. |
| Bubble Sort | O(n²) | Nested passes compare adjacent elements. |