1D Array Examples
Our visualization tool brings array operations to life through an intuitive graphical interface. When working with one-dimensional arrays, each element is represented as a rectangle, with the value displayed inside. The elements are arranged in a horizontal sequence, making it easy to understand the array's structure at a glance.
A key feature of our tool is its dynamic index tracking. As your code executes array operations like accessing or modifying elements, the tool automatically detects these actions and highlights the relevant element. The highlight smoothly transitions between elements as the index changes, helping you understand how your code traverses through the array.
This visual feedback makes it particularly useful for learning array manipulation concepts or debugging array-related algorithms.
Let's look at some common array algorithms and their visualizations:
Sort algorithms
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
bubble_sort([64, 34, 25, 12, 22, 11, 90])
def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
selection_sort([64, 34, 25, 12, 22, 11, 90])
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
insertion_sort([64, 34, 25, 12, 22, 11, 90])
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
shell_sort([64, 34, 25, 12, 22, 11, 90])
def counting_sort(arr):
max_val = max(arr)
count = [0] * (max_val + 1)
# Count occurrences
for num in arr:
count[num] += 1
# Reconstruct the sorted array
idx = 0
for i in range(len(count)):
while count[i] > 0:
arr[idx] = i
idx += 1
count[i] -= 1
counting_sort([3, 2, 3, 4, 6, 5, 1, 2, 4, 5])
Generating interactive preview...
Search Algorithms
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Example usage on sorted array
binary_search([11, 12, 22, 25, 34, 64, 90, 100], 90)
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
linear_search([64, 34, 25, 12, 22, 11, 90], 25)
Generating interactive preview...
Sliding Window Algorithms
def max_average(arr, k):
# Find maximum average of k consecutive elements
window_sum = sum(arr[:k])
max_avg = window_sum / k
for i in range(k, len(arr)):
window_sum = window_sum - arr[i-k] + arr[i]
max_avg = max(max_avg, window_sum / k)
return max_avg
max_average([1, 12, -5, -6, 50, 3], 4)
def longest_subarray(arr, target):
# Find longest subarray with sum <= target
left = curr_sum = max_len = 0
for right in range(len(arr)):
curr_sum += arr[right]
while curr_sum > target:
curr_sum -= arr[left]
left += 1
max_len = max(max_len, right - left + 1)
return max_len
longest_subarray([1, 2, 3, 4, 5], 10)
Generating interactive preview...
Array Manipulation Techniques
def build_prefix_sum(arr):
prefix = [0] * (len(arr) + 1)
for i in range(len(arr)):
prefix[i + 1] = prefix[i] + arr[i]
return prefix
def range_sum(prefix, left, right):
return prefix[right + 1] - prefix[left]
# Example usage
arr = [1, 2, 3, 4, 5]
prefix = build_prefix_sum(arr)
range_sum(prefix, 1, 3) # Sum of elements from index 1 to 3
def rotate_array(arr, k):
def reverse(arr, start, end):
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
k = k % len(arr)
reverse(arr, 0, len(arr)-1)
reverse(arr, 0, k-1)
reverse(arr, k, len(arr)-1)
rotate_array([1, 2, 3, 4, 5, 6, 7], 3)
Generating interactive preview...