Computer Science, Simply Explained.
Linear Data Structures, Part 3
Contents
Array
An array is a linear data structure that holds a fixed number of similar items. The elements in arrays are either ordered or unordered and are referenced by numerical indices.
To access an element in an array, you refer to it by its index. In the unordered array above, index 2 holds the letter “B”. The first index in an array is always the number 0.
Arrays have many operations that can be performed on them.
- Insertion: Add a new element anywhere in the array
- Deletion: Remove an element from anywhere the array
- Update: Update an element in the array
- Traversal: Scan through every element of the array
- Search: Scan through the array (traversal) to find a specific element
- Sort: Arrange the elements in a specific order
- Merge: Combine two similar arrays into one
To find an element in an unordered array, you scan through each element starting at index 0 until it matches what you are looking for.
Finding an element in an ordered array is a little bit quicker. In this example, we are looking for the letter “D”. Start by accessing the middle element, index 2, which holds the letter C. If the letter comes after “D” alphabetically, the element you’re looking for will be in the smaller indices. In this case, the letter comes before “D”. To find “D”, traverse through the elements with indices larger than index 2.