What is an Array?
An Array is a linear data structure that collects elements of the same data type and stores them in contiguous and adjacent memory locations. It is one of the most fundamental and widely used data structures in computer science.
Time Complexity
- Access (by Index)O(1)
- Search (Linear)O(n)
- Search (Binary, Sorted)O(log n)
- Insertion (at end, dynamic)O(1)*
- Insertion/Deletion (at index)O(n)
* Amortized time complexity for dynamic arrays.
Static vs. Dynamic Arrays
- Static Arrays: Fixed size determined at compile time. Fast and simple, but inflexible (e.g., C-style arrays).
- Dynamic Arrays: Automatically resize when full. Flexible but have slight overhead during resizing (e.g., Python Lists, Java ArrayList, C++ Vectors).