Quick Answer
Arrays in data structure store items in a row of memory. Every item has an index that starts at 0. Reading by index takes O(1) time. Inserting or deleting in the middle takes O(n) time. This post shows 3 ways to use arrays. They are a Python list, the array module and C++. Python and C++ code below was executed.
Arrays are the first data structure to learn. Most of DSA builds on them. Tutorials say that opening an item by index is instant and few say why.
Sections below explain the address math behind arrays in data structure and then test it with code. Examples show core array operations.
What Is an Array in Data Structure?
Arrays in data structure are collections of items stored in a row. Every item sits in a numbered slot. That number is the index and it starts at 0 and not 1.
Arrays of 5 numbers have indexes 0 to 4. First item is at index 0 and the last is at index 4. This off by one start trips up many beginners so fix it in your head early.
Items in an array share one type like all numbers or all words in most languages. See the array lesson and the DSA roadmap for the learning order.
Why Does Opening an Array Item by Index Take O(1) Time?
Writing arr[3] does not search for the fourth item. Computers calculate where that item lives in 1 step.
Arrays sit in one solid stretch of memory. Computers know the start address called the base and the bytes per item. Address of an item is the base plus the index times the item size.
For arr[3] the computer jumps forward 3 item sizes. No scanning or looping happens at any array size. That one calculation makes index access O(1) in every array data structure.
What Does Inserting Into an Array Cost?
Items in an array are packed with no gaps. New items cannot drop into the middle because there is no room. Every item after the spot shifts 1 slot right.
Deleting works the other way. Items after the gap shift left to close it. Both cases cost O(n) time.
Test below inserted 1 value at the front of a Python list and appended 1 value at the end. Table lists the lowest of 5 trials in CPython 3.13.18.
| Items in the list | Insert at front | Append at end |
|---|---|---|
| 1,000 | 0.2 µs | 33 ns |
| 100,000 | 17.1 µs | 33 ns |
| 1,000,000 | 462.3 µs | 34 ns |
Insert cost grew with the list while append stayed near 33 ns for these array operations. This single cost explains why other structures exist. Linked lists give up index access to gain cheap inserts.
How Do You Code Arrays in Python and C++?
Examples below use the numbers 4, 8, 15, 16 and 23.
Approach #1: Python List
What you get. Index access, edits, append, insert and delete in a few lines. Lists grow on their own.
arr = [4, 8, 15, 16, 23]
print(arr[2])
arr[2] = 42
arr.append(99)
print(arr, len(arr))
arr.insert(0, 1)
print(arr)
del arr[1]
print(arr)
Output is 15 then [4, 8, 42, 16, 23, 99] and 6. Insert at the front gives [1, 4, 8, 42, 16, 23, 99] and deleting index 1 gives [1, 8, 42, 16, 23, 99].
Python lists store references to objects. Empty lists took 56 bytes and every item added 8 bytes in the test. List size grew in steps at lengths 1, 5, 9, 17, 25 and 33 because Python makes a bigger array and copies items over.
Approach #2: Python Array Module
What you get. Fixed type array that stores raw values. Address math from above can be checked directly.
import ctypes
from array import array
numbers = array("i", [4, 8, 15, 16, 23])
base = numbers.buffer_info()[0]
size = numbers.itemsize
index = 3
address = base + index * size
print(size, address - base)
print(ctypes.c_int.from_address(address).value, numbers[index])
Output is 4 and 12 because an i item takes 4 bytes and index 3 sits 12 bytes from the base.
Next line is 16 and 16. Value taken from the computed address equals numbers[3].
Approach #3: C++ Array and Vector
What you get. Fixed size array and a vector that grows. Both compile with a main function.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int arr[5] = {4, 8, 15, 16, 23};
cout << arr[2] << endl;
arr[2] = 42;
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << (char*)&arr[3] - (char*)&arr[0] << endl;
vector<int> items = {4, 8, 15};
items.push_back(16);
cout << items.size() << endl;
}
Output is 15 then 4 8 42 16 23 then 12 then 4. Value 12 is the byte distance between index 0 and index 3. Basic C++ arrays have a fixed size while a vector adds items with push_back.
Tested. Python 3.13.18 executed both Python programs and fetched the value from the computed address. g++ 13.3 compiled the C++ version with warnings on. Tests ran in a cloud sandbox.
What Is the Time Complexity of Array Operations?
| Operation | Time | Why |
|---|---|---|
| Access or edit by index | O(1) | Address math |
| Append at the end | O(1) on average | Spare room exists in most cases |
| Insert or delete in the middle | O(n) | Items shift |
| Search an unsorted array | O(n) | Every item can need a check |
Space for n items is O(n) across all array operations. See linear search for the search case and time complexity for the notation.
Where Are Arrays in Data Structure Used?
- Ordered lists. Scores in a game or songs in a playlist fit a row of slots.
- Sorting. Selection sort works on arrays by index.
- Other structures. Stacks and queues can sit on top of arrays.
Practice arrays in data structure in the array practice set and repeat the array operations from this post.
Frequently Asked Questions
Why Do Array Indexes Start at 0?
Indexes are distances from the start. First item is 0 steps from the base address so its index is 0. That makes the address math direct.
How Costly Is Fetching an Array Element?
Cost is O(1). Computers calculate the address from the index in 1 step. Array size does not matter.
Why Is Adding Items in the Middle Slow?
Items are packed with no gaps. Every item after the spot must shift over to make room. In the test inserting at the front of 1,000,000 items took 462.3 µs.
How Does an Array Differ From a List in Python?
Python lists are flexible arrays of references that can grow and contain mixed types. Array module items are raw values of one type.
Can an Array Grow After Creation?
Basic C++ arrays have a fixed size. Python lists resize by making a bigger array and copying items over.
When Should You Avoid an Array?
Avoid it when you insert or delete in the middle many times. Shifting costs O(n) every time. Linked lists handle that case with cheap inserts.
How Does the Computer Find an Item by Index?
It adds the index times the item size to the base address. In the test items of 4 bytes put index 3 at 12 bytes from the base.
What Should You Remember About Array Operations?
Arrays in data structure are numbered slots in one stretch of memory. That layout makes index access O(1) and middle inserts O(n).
Key takeaways.
- Address is the base plus the index times the item size.
- Inserting or deleting in the middle shifts items.
- Appending at the end costs O(1) on average for arrays in data structure and for Python lists.
Check yourself. In [10, 20, 30, 40, 50] what value sits at index 3 and how many items shift for an insert at index 0? Answer is 40 and all 5 items shift.