DSAverse
Basics & Data Structures
Loading Data Structures...
Array Access
Array
Stack
Queue
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Basics & Data Structures
Loading Data Structures...
Array Access
Array
Stack
Queue
Watch a dynamic array grow, shrink, and shift elements. Understand how Python lists, Java ArrayLists, and C++ vectors work under the hood.
Filled cells = data, dashed cells = reserved capacity. Watch it resize!
Color Legend
Use Insert, Delete, Search, or Access to begin visualization.
*Append is O(1) amortized — rare O(n) resize amortizes over many O(1) appends.
Shift elements right, insert at index. Resize if full.
Remove element at index, shift remaining elements left.
Direct index calculation — O(1) random access.
Linear scan from index 0 until found or end.
When capacity doubles at sizes 4 → 8 → 16 → 32…:
What happens when you insert into a full dynamic array?
class DynamicArray:
def __init__(self, initial_capacity=4):
self.data = [None] * initial_capacity
self.size = 0
self.capacity = initial_capacity
def _resize(self):
old_cap = self.capacity
self.capacity *= 2 # Double the capacity
new_data = [None] * self.capacity
for i in range(self.size): # Copy all elements
new_data[i] = self.data[i]
self.data = new_data
def insert(self, index, value):
if index < 0 or index > self.size:
raise IndexError("Index out of bounds")
if self.size >= self.capacity:
self._resize() # Expand before inserting
for i in range(self.size, index, -1):
self.data[i] = self.data[i - 1]
self.data[index] = value
self.size += 1
def delete(self, index):
if index < 0 or index >= self.size:
raise IndexError("Index out of bounds")
value = self.data[index]
for i in range(index, self.size - 1):
self.data[i] = self.data[i + 1]
self.size -= 1
return value
def search(self, value):
for i in range(self.size):
if self.data[i] == value:
return i
return -1 # Not found
def access(self, index): # O(1) — direct address calculation
return self.data[index]
def append(self, value):
self.insert(self.size, value)