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.
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Find minimum value to assign all array elements so that array product becomes greater
- Generate original array from an array that store the counts of greater elements on right
- Print modified array after multiple array range increment operations
- Sort an array where a subarray of a sorted array is in reverse order
- Maximum subarray sum in array formed by repeating the given array k times
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Check if it is possible to make array increasing or decreasing by rotating the array
- Find K such that array A can be converted into array B by adding K to a selected range [L, R]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.