Parallel Array: Also known as structure an array (SoA), multiple arrays of the same size such that i-th element of each array is closely related and all i-th elements together represent an object or entity. An example parallel array is two arrays that represent x and y co-ordinates of n points.
Below is another example where we store the first name, last name and heights of 5 people in three different arrays.
first_name = ['Bones', 'Welma', 'Frank', 'Han', 'Jack'] last_name = ['Smith', 'Seger', 'Mathers', 'Solo', 'Jackles'] height = [169, 158, 201, 183, 172]
This way we could easily store the information and for accessing, the first index of each array corresponds to the data belonging to the same person.
Two of the most essential applications performs on an array or a record are searching and sorting.
- Searching: Each index in a parallel array corresponds to the data belonging to the same entity in a record. Thus, for searching an entity based on a specific value of an attribute, e.g. we need to find the name of a person having height >200 cms in the above example. Thus, we search for the index in the height array having value greater than 200. Now, once we have obtained the index, we print the values of the index in the first_name and last_name arrays. This way searching becomes an easy task in parallel array.
- Sorting: Now, using the same fact that each index corresponds to data items in different arrays corresponding to the same entity. Thus, we sort all arrays based on the same criteria. For example, in the above-displayed example, we need to sort the people in increasing order of their respective heights. Thus, when we swap the two heights, we even swap the corresponding values in other arrays using the same index.
Following steps are performed to search an entity based on a specific attribute in structure of array or parallel array.
- Search for the value of the required attribute in the respective array (e.g. search for values greater than 200 in the above example) using either of the linear search/binary search approaches.
- Store the index where the following values are obtained in the array
- Print the values at the evaluated indices in other arrays
The arrays can be sorted in any way, numerical, alphabetical or any other way but the elements are placed at equally spaced addresses. Any of the sorting techniques can be used to sort the record based on a specified criteria. Following steps are performed to sort a record.
- Find the index in the array (the array according to which sorting shall be done is based on the specified criteria) where the sequence voids (i.e. we need to compute those two indices where the sorting sequence fails, which is to be corrected by swapping the values at those indices).
- Now swap the values of those two calculated indices in all the arrays.
Thus, following the above steps, once the chosen array (the array to be chosen on a specified criterion) is sorted, all the arrays shall be sorted with respect to the chosen array.
1) The code below stores the first name, second name, and height of 10 students.
2) Sorts them in increasing order of the height using quicksort algorithm.
3) Searches the name of the 2nd tallest student, the 3rd shortest student and the student having a height equal to 158 cms in the record.
Name of people in increasing order of their height: Jinny Weasly has height 152 cms Welma Seger has height 158 cms Harry Potter has height 160 cms Emma Watson has height 163 cms Bones Smith has height 169 cms Jack Jackles has height 172 cms Tony Stark has height 173 cms Han Solo has height 183 cms Sherlock Holmes has height 185 cms Frank Mathers has height 201 cms Name of the second tallest person is Sherlock Holmes Name of the third shortest person is Harry Potter Person having height 158 cms is Welma Seger
- They can be used in languages which support only arrays of primitive types and not of records (or perhaps don’t support records at all).
- Parallel arrays are simple to understand and use, and are often used where declaring a record is more trouble than it’s worth.
- They can save a substantial amount of space in some cases by avoiding alignment issues.
- If the number of items is small, array indices can occupy significantly less space than full pointers, particularly on architectures with large words.
- Sequentially examining a single field of each record in the array is very fast on modern machines, since this amounts to a linear traversal of a single array, exhibiting ideal locality of reference and cache behavior.
- They have significantly worse locality of reference when visiting the records non-sequentially and examining multiple fields of each record.
- They have little direct language support (the language and its syntax typically express no relationship between the arrays in the parallel array).
- They are expensive to grow or shrink since each of several arrays must be reallocated. Multi-level arrays can ameliorate this problem, but impacts performance due to the additional indirection needed to find the desired elements.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.