Static arrays are fixed-size containers storing elements of the same data type in contiguous memory locations. In Python, traditional arrays are implemented through specialized modules like array
and numpy
.
from array import array
int_array = array('i', [1, 2, 3, 4, 5])
float_array = array('d', [1.0, 2.0, 3.0])
import sys
from array import array
numbers_list = [1, 2, 3, 4, 5]
numbers_array = array('i', [1, 2, 3, 4, 5])
print(f"List size: {sys.getsizeof(numbers_list)} bytes")
print(f"Array size: {sys.getsizeof(numbers_array)} bytes")
def access_element(arr, index):
if 0 <= index < len(arr):
return arr[index]
raise IndexError("Index out of bounds")
arr = array('i', [1, 2, 3, 4, 5])
element = access_element(arr, 2)
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
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
def insert_at_index(arr, index, element):
"""
Insert element at specific index
Time Complexity: O(n)
"""
if not isinstance(arr, list):
arr = list(arr)
if 0 <= index <= len(arr):
arr.insert(index, element)
return array('i', arr)
raise IndexError("Invalid index")
def delete_at_index(arr, index):
"""
Delete element at specific index
Time Complexity: O(n)
"""
if not isinstance(arr, list):
arr = list(arr)
if 0 <= index < len(arr):
arr.pop(index)
return array('i', arr)
raise IndexError("Invalid index")
def rotate_array(arr, k):
"""
Rotate array by k positions
Time Complexity: O(n)
Space Complexity: O(1)
"""
n = len(arr)
k = k % n
def reverse(start, end):
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
reverse(0, n-1)
reverse(0, k-1)
reverse(k, n-1)
return arr
def max_subarray(arr):
"""
Find maximum sum subarray using Kadane's Algorithm
Time Complexity: O(n)
Space Complexity: O(1)
"""
max_sum = current_sum = arr[0]
for num in arr[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
def rearrange_array(arr):
"""
Rearrange array so that arr[i] becomes arr[arr[i]]
Time Complexity: O(n)
Space Complexity: O(1)
"""
n = len(arr)
for i in range(n):
arr[i] += (arr[arr[i]] % n) * n
for i in range(n):
arr[i] //= n
return arr
def cache_friendly_traverse(arr):
"""
Traverse array in a cache-friendly manner
"""
CACHE_LINE_SIZE = 64
ELEMENTS_PER_LINE = CACHE_LINE_SIZE // 4
n = len(arr)
for i in range(0, n, ELEMENTS_PER_LINE):
end = min(i + ELEMENTS_PER_LINE, n)
for j in range(i, end):
pass
import numpy as np
def vectorized_operations(arr):
"""
Perform vectorized operations using NumPy
"""
np_arr = np.array(arr)
squared = np_arr ** 2
doubled = np_arr * 2
filtered = np_arr[np_arr > 5]
return squared, doubled, filtered
import numpy as np
def create_aligned_array(size):
"""
Create memory-aligned array using NumPy
"""
aligned_array = np.zeros(size, dtype=np.int32, align=64)
return aligned_array
def safe_array_access(arr, index):
"""
Safely access array elements
"""
try:
return arr[index]
except IndexError:
return None
def memory_efficient_array():
"""
Create memory-efficient array for integers
"""
int_array = array('i', range(1000))
return int_array