Data Structure: Arrays
The array data structure is one of the most commonly used data structures. It is simple in design but highly reliable in practice, and it is used in more situations than any other data structure. Nevertheless, it has its challenges, which can occasionally harm the system's performance.
Read on: Operations on Array Part 1 & Part 2
What is an Array
Arrays are collections of elements that share a common data type and are stored in contiguous memory locations. It allows direct access to elements using an index, which identifies specific elements within the array. The elements of an array can be of any type, including numbers, strings, and objects. The length of an array is fixed and cannot be changed after creation. Arrays are commonly used to store and manipulate large amounts of data.
Where arrays are used?
Arrays are used in a wide variety of applications. Listing them would take a long time; I have chosen a few to illustrate my point.
Storing and accessing sequential data: An array can be used to store and access sequential data elements, such as movie or product ratings, stock prices, employee records, etc.,
Sorting and Searching Algorithms: The use of arrays is common in algorithms that involve sorting and searching information. In addition, the fact that they are indexed makes it possible to implement classic algorithms efficiently.
String Manipulation: Strings are often represented as arrays of characters in programming languages. As a result, individual characters can be manipulated, accessed, and processed efficiently.
Lookup Tables and Caching: Arrays are used where the index corresponds to the key for fast lookups. Hash maps and caches are typical examples of lookup tables implemented using arrays.
Graphs and Trees (Adjacency Matrices): Arrays can represent graphs and tree structures, particularly compact representations of connections between nodes.
Handling Multidimensional Data: Arrays (often referred to as tensors or multidimensional arrays) represent data with more than two dimensions in domains such as machine learning and scientific computing.
Structure of Array
Arrays are finite collections of similar elements located adjacently in memory. Arrays containing n elements are referenced by indices ranging from 0 to n - 1. As an example, the elements of an array arr[n] containing n elements are denoted as arr[0], arr[1], arr[2], ...., arr[n-1], where 0 is the lower bound and n-1 is the upper bound. In this example, 0, 1, 2, etc., represent array indices.
An array can be operated upon in several different ways. The following tables provide a list of operations.
Traversal: Each array element is accessed and visited individually, usually in a loop (e.g., for, while, or forEach).
Insertion: A new element is added at a specific position in the array. Some languages require resizing arrays if their capacity exceeds their maximum size.
Deletion: Removing an element from an array may be followed by resizing the array or shifting the remaining elements to maintain its order.
Searching: Locating an element in an array by its index. The search can be linear or binary, depending on whether the array is sorted.
Sorting: Rearranging the elements of an array in an ascending or descending order. Sorting algorithms such as Bubble Sort, Merge Sort, or Quick Sort are used to maximize efficiency.
Merging: Combining two or more arrays into a single array.
Reversing: Reversing the order of the elements in the array so that the last element becomes the first element and vice versa.
Filtering: A new array is created with elements that satisfy a predicate function.
Mapping: A new array is created by applying a function to each original array element.
Reducing: By repeatedly applying a function, all elements of an array are aggregated into a single value.