DSAverse
Initializing Sorting Algorithms...
Sorting Algorithm
Binary Tree
Graph Traversal
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Explore how dynamic arrays work with automatic resizing, insertion, deletion, and search operations. Understanding the foundation of ArrayList, Vector, and Python lists.
Select an operation to begin visualization
Add element at specific index, shift others right
Remove element at index, shift others left
Direct access by index calculation
Linear scan through elements
class DynamicArray:
def __init__(self, initial_capacity=4):
self.data = [None] * initial_capacity # Fixed-size array
self.size = 0 # Number of elements
self.capacity = initial_capacity # Current capacity
def _resize(self):
# Double the capacity when array is full
old_capacity = self.capacity
self.capacity *= 2
new_data = [None] * self.capacity
# Copy existing elements
for i in range(self.size):
new_data[i] = self.data[i]
self.data = new_data
print(f"Resized from {old_capacity} to {self.capacity}")
def insert(self, index, value):
# Validate index
if index < 0 or index > self.size:
raise IndexError("Index out of bounds")
# Resize if necessary
if self.size >= self.capacity:
self._resize()
# Shift elements to the right
for i in range(self.size, index, -1):
self.data[i] = self.data[i - 1]
# Insert new element
self.data[index] = value
self.size += 1
def delete(self, index):
# Validate index
if index < 0 or index >= self.size:
raise IndexError("Index out of bounds")
# Get value to return
value = self.data[index]
# Shift elements to the left
for i in range(index, self.size - 1):
self.data[i] = self.data[i + 1]
self.size -= 1
return value
def search(self, value):
# Linear search through the array
for i in range(self.size):
if self.data[i] == value:
return i
return -1 # Not found
def access(self, index):
# Direct access by index
if index < 0 or index >= self.size:
raise IndexError("Index out of bounds")
return self.data[index]
def append(self, value):
# Insert at the end
self.insert(self.size, value)
def get_size(self):
return self.size
def get_capacity(self):
return self.capacity