An array is a fundamental data structure that stores elements in contiguous memory locations. Each element can be accessed directly using its index, making arrays one of the most efficient data structures for random access operations.
Arrays have a fixed size (in most languages) and provide O(1) time complexity for accessing elements by index. However, insertion and deletion operations may require shifting elements, resulting in O(n) time complexity in the worst case.
Initialize: Enter a size and click "Initialize" to create an array with null values.
Insert: Enter an index and value, then click "Insert" to add an element at that position.
Update: Enter an index and new value, then click "Update" to change an existing element.
Delete: Enter an index and click "Delete" to remove the element at that position.
Visualization: Watch elements shift when inserting or deleting to understand array behavior.
Initialize: Create a new array with a specified size (all elements set to "null")
Insert: Add an element at a specific position (shifts elements to the right)
Update: Change the value of an element at a given index
Delete: Remove an element at a position (shifts elements to the left)
Access by Index: O(1) - Direct memory address calculation from base address + index.
Search (Unsorted): O(n) - Linear scan through all elements in worst case.
Search (Sorted): O(log n) - Binary search divides search space in half each step.
Insert at End: O(1) - Direct placement if space available; O(n) if resizing needed.
Insert at Beginning/Middle: O(n) - Requires shifting all subsequent elements right.
Delete at End: O(1) - Simply decrement size counter.
Delete at Beginning/Middle: O(n) - Requires shifting all subsequent elements left.
vs Linked List: Arrays have O(1) random access but O(n) insertion/deletion; linked lists have O(n) access but O(1) insertion/deletion at known positions.
vs Stack/Queue: Arrays are general-purpose; stacks/queues are specialized with restricted access patterns (LIFO/FIFO).
vs Dynamic Array: Static arrays have fixed size; dynamic arrays (ArrayList, vector) resize automatically but may have occasional O(n) resizing cost.
vs Hash Table: Arrays use integer indices with O(1) access; hash tables use arbitrary keys with O(1) average access.
Best Use Case: When you need fast random access by index, know the size in advance, or need cache-friendly sequential traversal.
Image Processing: Pixel data stored as 2D arrays for manipulation and filtering.
Database Tables: Rows stored contiguously for fast sequential scans and indexing.
Game Development: Game boards, tile maps, and sprite sheets use 2D arrays.
Audio/Video Buffers: Streaming data stored in arrays for real-time processing.
Matrix Operations: Scientific computing, machine learning use arrays for linear algebra.
Lookup Tables: Precomputed values stored for O(1) retrieval (e.g., sine tables, color palettes).
Sorting Algorithms: Most sorting algorithms operate on arrays due to random access needs.
Two Pointers: Use left/right pointers moving toward each other for problems like pair sum, palindrome check, or container with most water.
Sliding Window: Maintain a window of elements for substring/subarray problems like max sum subarray or longest substring without repeats.
Prefix Sum: Precompute cumulative sums for O(1) range sum queries; useful in subarray sum problems.
Binary Search: O(log n) search on sorted arrays; also used for search space reduction in optimization problems.
Kadane's Algorithm: Find maximum subarray sum in O(n) by tracking local and global maximums.
Dutch National Flag: Three-way partitioning for problems like sort colors (0s, 1s, 2s) in single pass.
Merge Intervals: Sort by start time, then merge overlapping intervals; common in scheduling problems.
/* Simple array operations in C */
#include <stdio.h>
#include <stdlib.h>
/* Initialize an array with given size and default value */
int* initialize_array(int size, int default_value) {
int *arr = malloc(sizeof(int) * size);
if (!arr) return NULL;
for (int i = 0; i < size; i++) arr[i] = default_value;
return arr;
}
/* Insert value at position by shifting elements right (assumes enough capacity) */
void insert_at(int *arr, int *length, int capacity, int pos, int value) {
if (pos < 0 || pos > *length || *length >= capacity) return;
for (int i = *length; i > pos; i--) arr[i] = arr[i-1];
arr[pos] = value;
(*length)++;
}
/* Delete element at position by shifting left */
void delete_at(int *arr, int *length, int pos) {
if (pos < 0 || pos >= *length) return;
for (int i = pos; i < *length - 1; i++) arr[i] = arr[i+1];
(*length)--;
}
/* Example usage */
int main(void) {
int capacity = 10;
int length = 5;
int *arr = initialize_array(capacity, 0);
if (!arr) return 1;
// populate
for (int i = 0; i < length; i++) arr[i] = i + 1; // 1 2 3 4 5
insert_at(arr, &length, capacity, 2, 99); // inserts 99 at index 2
delete_at(arr, &length, 4); // deletes element at index 4
// print
for (int i = 0; i < length; i++) printf("%d ", arr[i]);
printf("
");
free(arr);
return 0;
}