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.
Application:
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.
Searching:
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
Sorting:
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.
Implementation:
- The code below stores the first name, second name, and height of 10 students.
- Sorts them in increasing order of the height using quicksort algorithm.
- 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.
// C++ implementation of parallel arrays // and operations on them #include <iostream> using namespace std;
/* This function takes the last element as pivot places the pivot element at its
correct position in a sorted array, and
places all smaller (smaller than pivot)
to left of the pivot and all greater elements
to right of pivot */
int partition(string first_name[], string
last_name[],
int height[], int low, int high)
{ int pivot = height[high]; // pivot
int i = (low - 1); // Index of smaller element
for ( int j = low; j <= high - 1; j++) {
// If current element is smaller than
// or equal to pivot. This means are
// sorting sequence condition fails if
// the condition becomes true. Thus the
// two indices which shall be obtained
// here will be i and j and therefore
// we will be swapping values of i and j
// in all the arrays.
if (height[j] <= pivot) {
// increment index of smaller element
i++;
// Swapping values of i and j in
// all the arrays
string temp = first_name[i];
first_name[i] = first_name[j];
first_name[j] = temp;
temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = temp;
int temp1 = height[i];
height[i] = height[j];
height[j] = temp1;
}
}
string temp = first_name[i + 1];
first_name[i + 1] = first_name[high];
first_name[high] = temp;
temp = last_name[i + 1];
last_name[i + 1] = last_name[high];
last_name[high] = temp;
int temp1 = height[i + 1];
height[i + 1] = height[high];
height[high] = temp1;
return (i + 1);
} // Function which implements quick sort void quickSort(string first_name[], string last_name[],
int height[], int low, int high)
{ if (low < high) {
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(first_name, last_name,
height, low, high);
// Separately sort elements before
// partition and after partition
quickSort(first_name, last_name, height,
low, pi - 1);
quickSort(first_name, last_name, height,
pi + 1, high);
}
} // Function which binary searches the height // array for value 158 and if found, prints // the corresponding value in other arrays // at that index. void binarySearch(string first_name[], string
last_name[],
int height[], int value, int n)
{ int low = 0, high = n - 1;
int index;
while (low <= high) {
index = (high + low) / 2;
if (height[index] == 158) {
// This index of height array
// corresponds to the name
// of the person in first name
// and last name array.
cout << "Person having height 158"
" cms is "
<< first_name[index]
<< " " << last_name[index] << endl;
return ;
}
else if (height[index] > 158)
high = index - 1;
else
low = index + 1;
}
cout << "Sorry, no such person with"
" height 158 cms" ;
cout << "is found in the record" ;
} // Printing same index of each array. This // will give meaningful data as in parallel // array indices point to values in different // arrays belonging to the same entity void printParallelArray(string first_name[],
string last_name[], int height[], int n)
{ cout << "Name of people in increasing" ;
cout << "order of their height: " << endl;
for ( int i = 0; i < n; i++) {
cout << first_name[i] << " "
<< last_name[i] << " has height "
<< height[i] << " cms\n" ;
}
cout << endl;
} // Driver Function int main()
{ // These arrays together form a set
// of arrays known as parallel array
int n = 10;
string first_name[] = { "Bones" , "Welma" ,
"Frank" , "Han" , "Jack" , "Jinny" , "Harry" ,
"Emma" , "Tony" , "Sherlock" };
string last_name[] = { "Smith" , "Seger" ,
"Mathers" , "Solo" , "Jackles" , "Weasly" ,
"Potter" , "Watson" , "Stark" , "Holmes" };
int height[] = { 169, 158, 201, 183, 172,
152, 160, 163, 173, 185 };
// Sorting the above arrays using quickSort
// technique based on increasing order of
// their heights.
quickSort(first_name, last_name, height,
0, n - 1);
printParallelArray(first_name, last_name,
height, n);
// Second tallest person in the sorted
// list will be the second person from the
// right end.
cout << "Name of the second tallest person"
" is "
<< first_name[n - 2] << " "
<< last_name[n - 2] << endl;
// Third shortest person in the sorted
// list will be the third person from the
// left end.
cout << "Name of the third shortest person is "
<< first_name[2] << " " << last_name[2]
<< endl;
// We binary search the height array to
// search for a person having height 158
// cms.
binarySearch(first_name, last_name, height,
158, n);
return 0;
} |
// Java implementation of parallel arrays // and operations on them import java.util.*;
import java.lang.*;
class parallel_array_java {
/* This function takes the last element as
pivot, places the pivot element at its
correct position in sorted array, and
places all smaller (smaller than pivot)
to left of pivot and all greater elements
to right of pivot */
static int partition(String first_name[],
String last_name[], int height[], int low,
int high)
{
int pivot = height[high]; // pivot
int i = (low - 1 ); // Index of smaller element
for ( int j = low; j <= high - 1 ; j++) {
// If current element is smaller than
// or equal to pivot. This means are
// sorting sequence condition fails if
// the condition becomes true. Thus the
// two indices which shall be obtained
// here will be i and jand therefore
// we will be swapping values of i and j
// in all the arrays.
if (height[j] <= pivot) {
i++; // increment index of smaller element
// Swapping values of i and j in all the arrays
String temp = first_name[i];
first_name[i] = first_name[j];
first_name[j] = temp;
temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = temp;
int temp1 = height[i];
height[i] = height[j];
height[j] = temp1;
}
}
String temp = first_name[i + 1 ];
first_name[i + 1 ] = first_name[high];
first_name[high] = temp;
temp = last_name[i + 1 ];
last_name[i + 1 ] = last_name[high];
last_name[high] = temp;
int temp1 = height[i + 1 ];
height[i + 1 ] = height[high];
height[high] = temp1;
return (i + 1 );
}
// Function which implements quick sort
static void quickSort(String first_name[],
String last_name[], int height[], int low,
int high)
{
if (low < high) {
/* pi is partitioning index, arr[p]
is now at right place */
int pi = partition(first_name, last_name,
height, low, high);
// Separately sort elements before
// partition and after partition
quickSort(first_name, last_name, height,
low, pi - 1 );
quickSort(first_name, last_name, height,
pi + 1 , high);
}
}
// Function which binary searches the height array
// for value 158 and if found, prints the
// corresponding value in other arrays at that index.
static void binarySearch(String first_name[],
String last_name[], int height[], int value, int n)
{
int low = 0 , high = n - 1 ;
int index;
while (low <= high) {
index = (high + low) / 2 ;
if (height[index] == 158 ) {
// This index of height array corresponds
// to the name of the person in first
// name and last name array.
System.out.println( "Person having height"
+ " 158 cms is " + first_name[index] + " " + last_name[index]);
return ;
}
else if (height[index] > 158 )
high = index - 1 ;
else
low = index + 1 ;
}
System.out.print( "Sorry, no such person"
+ " with height" );
System.out.println( "158 cms is found in"
+ " the record" );
}
// Printing same index of each array. This
// will give meaningful data as in parallel
// array indices point to the values in
// different arrays belonging to the same
// entity
static void printParallelArray(String first_name[],
String last_name[], int height[], int n)
{
System.out.print( "Name of people in increasing"
+ " order of" );
System.out.println( "their height: " );
for ( int i = 0 ; i < n; i++) {
System.out.println(first_name[i] + " " + last_name[i] + " has height " + height[i] + " cms" );
}
System.out.println();
}
public static void main(String args[])
{
// These arrays together form a set of arrays
// known as parallel array
int n = 10 ;
String[] first_name = { "Bones" , "Welma" ,
"Frank" , "Han" , "Jack" , "Jinny" , "Harry" ,
"Emma" , "Tony" , "Sherlock" };
String[] last_name = { "Smith" , "Seger" ,
"Mathers" , "Solo" , "Jackles" , "Weasly" ,
"Potter" , "Watson" , "Stark" , "Holmes" };
int [] height = { 169 , 158 , 201 , 183 , 172 ,
152 , 160 , 163 , 173 , 185 };
// Sorting the above arrays using quickSort
// technique based on increasing order of
// their heights.
quickSort(first_name, last_name, height,
0 , n - 1 );
printParallelArray(first_name, last_name,
height, n);
// Second tallest person in the sorted list
// will be the second person from the right end.
System.out.println( "Name of the second tallest"
+ "person is " + first_name[n - 2 ] + " " + last_name[n - 2 ]);
// Third shortest person in the sorted list
// will be the third person from the left end.
System.out.println( "Name of the third shortest"
+ " person is " + first_name[ 2 ] + " " + last_name[ 2 ]);
// We binary search the height array to
// search for a person having height 158 cms.
binarySearch(first_name, last_name, height, 158 , n);
}
} |
# Python3 implementation of parallel arrays and operations on them # This function takes the last element as pivot, places the pivot # element at its correct position in sorted array, and places all # smaller (smaller then pivot) to left of pivot and all greater # elements to right of pivot. def partition(first_name, last_name, height, low, high):
pivot = height[high] # pivot
i = low - 1 # index of smaller element
for j in range (low, high):
# If current element is smaller than
# or equal to pivot. This means are
# sorting sequence condition fails if
# the condition becomes true. Thus the
# two indices which shall be obtained
# here will be i and jand therefore
# we will be swapping values of i and j
# in all the arrays.
if height[j] < = pivot:
i + = 1 # Increment index of smaller element
# Swapping values of i and j in all the arrays
temp = first_name[i]
first_name[i] = first_name[j]
first_name[j] = temp
temp = last_name[i]
last_name[i] = last_name[j]
last_name[j] = temp
temp1 = height[i]
height[i] = height[j]
height[j] = temp1
temp = first_name[i + 1 ]
first_name[i + 1 ] = first_name[high]
first_name[high] = temp
temp = last_name[i + 1 ]
last_name[i + 1 ] = last_name[high]
last_name[high] = temp
temp1 = height[i + 1 ]
height[i + 1 ] = height[high]
height[high] = temp1
return i + 1
# Function which implements quick sort def quickSort(first_name, last_name, height, low, high):
if low < high:
# pi is partitioning index, arr[p] is now at right place
pi = partition(first_name, last_name, height, low, high)
# Separately sort elements before partition and after partition
quickSort(first_name, last_name, height, low, pi - 1 )
quickSort(first_name, last_name, height, pi + 1 , high)
# Function which binary searches the height array for value 158 and if found, # prints the corresponding value in other arrays at that index def binarySearch(first_name, last_name, height, value, n):
low, high = 0 , n - 1
while low < = high:
index = (high + low) / / 2
if height[index] is 158 :
# This index of height array corresponds to the name of the
# person in first name and last name array.
print ( "Person having height 158 cms is" ,
first_name[index], last_name[index])
return
elif height[index] > 158 :
high = index - 1
else :
low = index + 1
print ( "Sorry, no such person with height" )
print ( "158 cms is found in the record" )
# Printing same index of each array. this will give meaningful data # as in parallel array indices point to the values in different ways # belonging to the same entity. def printParallelArray(first_name, last_name, height, n):
print ( "Name of people in increasing order of their height" )
for i in range ( 0 , n):
print (first_name[i], last_name[i], "has height" , height[i], "cms" )
print ()
n = 10
first_name = [ "Bones" , "Welma" , "Frank" , "Han" , "Jack" ,
"Jinny" , "Harry" , "Emma" , "Tony" , "Sherlock" ]
last_name = [ "Smith" , "Seger" , "Mathers" , "Solo" , "Jackles" ,
"Weasly" , "Potter" , "Watson" , "Stark" , "Holmes" ]
height = [ 169 , 158 , 201 , 183 , 172 , 152 , 160 , 163 , 173 , 185 ]
# Sorting the above arrays using quickSort technique based on increasing # order of their heights quickSort(first_name, last_name, height, 0 , n - 1 )
printParallelArray(first_name, last_name, height, n) # Second tallest person in the sorted list will be the second person from # the right end print ( "Name of the second tallest person is" , first_name[n - 2 ], last_name[n - 2 ])
# Third shortest person in the sorted list will be the third person from # the left end. print ( "Name of the third shortest person is" , first_name[ 2 ], last_name[ 2 ])
# We binary search the height array to search for a person having height 158 cms. binarySearch(first_name, last_name, height, 158 , n)
|
// C# implementation of parallel arrays // and operations on them using System;
class GFG {
/* This function takes last element
as pivot, places the pivot element
at its correct position in sorted
array, and places all smaller
(smaller than pivot) to left of
pivot and all greater elements to
right of pivot */
static int partition(String[] first_name,
String[] last_name,
int [] height, int low,
int high)
{
// pivot
int pivot = height[high];
// Index of smaller element
int i = (low - 1);
for ( int j = low; j <= high - 1; j++)
{
// If current element is smaller
// than or equal to pivot. This
// means are sorting sequence
// condition fails if the condition
// becomes true. Thus the two
// indices which shall be obtained
// here will be i and jand therefore
// we will be swapping values of i
// and j in all the arrays.
if (height[j] <= pivot) {
// increment index of smaller
// element
i++;
// Swapping values of i and j
// in all the arrays
String temp = first_name[i];
first_name[i] = first_name[j];
first_name[j] = temp;
temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = temp;
int temp1 = height[i];
height[i] = height[j];
height[j] = temp1;
}
}
String tempp = first_name[i + 1];
first_name[i + 1] = first_name[high];
first_name[high] = tempp;
tempp = last_name[i + 1];
last_name[i + 1] = last_name[high];
last_name[high] = tempp;
int temp2 = height[i + 1];
height[i + 1] = height[high];
height[high] = temp2;
return (i + 1);
}
// Function which implements quick sort
static void quickSort(String[] first_name,
String[] last_name,
int [] height, int low,
int high)
{
if (low < high) {
/* pi is partitioning index,
arr[p] is now at right place */
int pi = partition(first_name,
last_name, height,
low, high);
// Separately sort elements before
// partition and after partition
quickSort(first_name, last_name,
height, low, pi - 1);
quickSort(first_name, last_name,
height, pi + 1, high);
}
}
// Function which binary searches the
// height array for value 158 and if
// found, prints the corresponding value
// in other arrays at that index.
static void binarySearch(String[] first_name,
String[] last_name,
int [] height, int value,
int n)
{
int low = 0, high = n - 1;
int index;
while (low <= high) {
index = (high + low) / 2;
if (height[index] == 158) {
// This index of height array
// corresponds to the name of
// the person in first name and
// last name array.
Console.Write( "Person having height"
+ " 158 cms is " +
first_name[index] +
" " + last_name[index]);
return ;
}
else if (height[index] > 158)
high = index - 1;
else
low = index + 1;
}
Console.Write( "Sorry, no such person"
+ " with height 158 cms is "
+ "found in the record" );
}
// Printing same index of each array. This
// will give meaningful data as in parallel
// array indices point to the values in
// different arrays belonging to the same
// entity
static void printParallelArray(String[] first_name,
String[] last_name,
int [] height, int n)
{
Console.WriteLine( "Name of people in increasing"
+ " order of their height: " );
for ( int i = 0; i < n; i++) {
Console.WriteLine(first_name[i] + " " +
last_name[i] + " has height " +
height[i] + " cms" );
}
Console.WriteLine();
}
// Driver function
public static void Main()
{
// These arrays together form a set of
// arrays known as parallel array
int n = 10;
String[] first_name = { "Bones" , "Welma" ,
"Frank" , "Han" , "Jack" ,
"Jinny" , "Harry" , "Emma" ,
"Tony" , "Sherlock" };
String[] last_name = { "Smith" , "Seger" ,
"Mathers" , "Solo" , "Jackles" ,
"Weasly" , "Potter" , "Watson" ,
"Stark" , "Holmes" };
int [] height = { 169, 158, 201, 183, 172,
152, 160, 163, 173, 185 };
// Sorting the above arrays using quickSort
// technique based on increasing order of
// their heights.
quickSort(first_name, last_name, height,
0, n - 1);
printParallelArray(first_name, last_name,
height, n);
// Second tallest person in the sorted list
// will be the second person from the right end.
Console.WriteLine( "Name of the second tallest"
+ "person is " + first_name[n - 2]
+ " " + last_name[n - 2]);
// Third shortest person in the sorted list
// will be the third person from the left end.
Console.WriteLine( "Name of the third shortest"
+ " person is " + first_name[2]
+ " " + last_name[2]);
// We binary search the height array to
// search for a person having height 158 cms.
binarySearch(first_name, last_name, height,
158, n);
}
} // This codecontribute by parashar. |
<?php // PHP implementation of parallel // arrays and operations on them /* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ function partition(& $first_name , & $last_name ,
& $height , $low , $high )
{ $pivot = $height [ $high ]; // pivot
$i = ( $low - 1); // Index of smaller element
for ( $j = $low ; $j <= $high - 1; $j ++)
{
// If current element is smaller
// than or equal to pivot. This
// means are sorting sequence
// condition fails if the condition
// becomes true. Thus the two indices
// which shall be obtained here will
// be i and j and therefore we will
// be swapping values of i and j
// in all the arrays.
if ( $height [ $j ] <= $pivot )
{
// increment index of
// smaller element
$i ++;
// Swapping values
// of i and j in
// all the arrays
$temp = $first_name [ $i ];
$first_name [ $i ] = $first_name [ $j ];
$first_name [ $j ] = $temp ;
$temp = $last_name [ $i ];
$last_name [ $i ] = $last_name [ $j ];
$last_name [ $j ] = $temp ;
$temp1 = $height [ $i ];
$height [ $i ] = $height [ $j ];
$height [ $j ] = $temp1 ;
}
}
$temp = $first_name [ $i + 1];
$first_name [ $i + 1] = $first_name [ $high ];
$first_name [ $high ] = $temp ;
$temp = $last_name [ $i + 1];
$last_name [ $i + 1] = $last_name [ $high ];
$last_name [ $high ] = $temp ;
$temp1 = $height [ $i + 1];
$height [ $i + 1] = $height [ $high ];
$height [ $high ] = $temp1 ;
return ( $i + 1);
} // Function which // implements quick sort function quickSort(& $first_name , & $last_name ,
& $height , $low , $high )
{ if ( $low < $high )
{
/* pi is partitioning
index, arr[p] is now
at right place */
$pi = partition( $first_name , $last_name ,
$height , $low , $high );
// Separately sort elements
// before partition and
// after partition
quickSort( $first_name , $last_name ,
$height , $low , $pi - 1);
quickSort( $first_name , $last_name ,
$height , $pi + 1, $high );
}
} // Function which binary searches // the height array for value 158 // and if found, prints the // corresponding value in other // arrays at that index. function binarySearch(& $first_name , & $last_name ,
& $height , $value , $n )
{ $low = 0; $high = $n - 1;
$index = 0;
while ( $low <= $high )
{
$index = ( $high + $low ) / 2;
if ( $height [ $index ] == 158)
{
// This index of height array
// corresponds to the name
// of the person in first name
// and last name array.
echo ( "Person having height 158" .
" cms is " . $first_name [ $index ] .
" " . $last_name [ $index ] . "\n" );
return ;
}
else if ( $height [ $index ] > 158)
$high = $index - 1;
else
$low = $index + 1;
}
echo ( "Sorry, no such person with" .
" height 158 cms" );
echo ( "is found in the record" );
} // Printing same index of each // array. This will give meaningful // data as in parallel array indices // po$to values in different arrays // belonging to the same entity function printParallelArray(& $first_name ,
& $last_name ,
& $height , & $n )
{ echo ( "Name of people in increasing" );
echo ( " order of their height: " . "\n" );
for ( $i = 0; $i < $n ; $i ++)
{
echo ( $first_name [ $i ] . " " .
$last_name [ $i ] . " has height " .
$height [ $i ] . " cms\n" );
}
echo ( "\n" );
} // Driver Code // These arrays together // form a set of arrays // known as parallel array $n = 10;
$first_name = array ( "Bones" , "Welma" ,
"Frank" , "Han" ,
"Jack" , "Jinny" ,
"Harry" , "Emma" ,
"Tony" , "Sherlock" );
$last_name = array ( "Smith" , "Seger" ,
"Mathers" , "Solo" ,
"Jackles" , "Weasly" ,
"Potter" , "Watson" ,
"Stark" , "Holmes" );
$height = array (169, 158, 201, 183, 172,
152, 160, 163, 173, 185);
// Sorting the above arrays // using quickSort technique // based on increasing order // of their heights. quickSort( $first_name , $last_name ,
$height , 0, $n - 1);
printParallelArray( $first_name ,
$last_name ,
$height , $n );
// Second tallest person in // the sorted list will be // second person from the // right end. echo ( "Name of the second " .
"tallest person is " .
$first_name [ $n - 2] .
" " . $last_name [ $n - 2] . "\n" );
// Third shortest person in // the sorted list will be // third person from the // left end. echo ( "Name of the third shortest person is " .
$first_name [2] . " " . $last_name [2] . "\n" );
// We binary search the height // array to search for a person // having height 158 cms. binarySearch( $first_name , $last_name ,
$height , 158, $n );
// This code is contributed by // Manish Shaw(manishshaw1) ?> |
<script> // Javascript implementation of parallel arrays // and operations on them /* This function takes the last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ function partition(first_name, last_name, height, low, high) {
let pivot = height[high]; // pivot
let i = (low - 1); // Index of smaller element
for (let j = low; j <= high - 1; j++) {
// If current element is smaller than
// or equal to pivot. This means are
// sorting sequence condition fails if
// the condition becomes true. Thus the
// two indices which shall be obtained
// here will be i and jand therefore
// we will be swapping values of i and j
// in all the arrays.
if (height[j] <= pivot) {
i++; // increment index of smaller element
// Swapping values of i and j in all the arrays
let temp = first_name[i];
first_name[i] = first_name[j];
first_name[j] = temp;
temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = temp;
let temp1 = height[i];
height[i] = height[j];
height[j] = temp1;
}
}
let temp = first_name[i + 1];
first_name[i + 1] = first_name[high];
first_name[high] = temp;
temp = last_name[i + 1];
last_name[i + 1] = last_name[high];
last_name[high] = temp;
let temp1 = height[i + 1];
height[i + 1] = height[high];
height[high] = temp1;
return (i + 1);
} // Function which implements quick sort function quickSort(first_name, last_name, height, low, high) {
if (low < high) {
/* pi is partitioning index, arr[p]
is now at right place */
let pi = partition(first_name, last_name,
height, low, high);
// Separately sort elements before
// partition and after partition
quickSort(first_name, last_name, height,
low, pi - 1);
quickSort(first_name, last_name, height,
pi + 1, high);
}
} // Function which binary searches the height array // for value 158 and if found, prints the // corresponding value in other arrays at that index. function binarySearch(first_name, last_name, height, value, n) {
let low = 0, high = n - 1;
let index;
while (low <= high) {
index = Math.floor((high + low) / 2);
if (height[index] == 158) {
// This index of height array corresponds
// to the name of the person in first
// name and last name array.
document.write( "Person having height"
+ " 158 cms is " + first_name[index] + " " + last_name[index] + "<br>" );
return ;
}
else if (height[index] > 158)
high = index - 1;
else
low = index + 1;
}
document.write( "Sorry, no such person"
+ " with height" + "<br>" );
document.write( "158 cms is found in"
+ " the record <br>" );
} // Printing same index of each array. This // will give meaningful data as in parallel // array indices point to the values in // different arrays belonging to the same // entity function printParallelArray(first_name, last_name, height, n) {
document.write( "Name of people in increasing"
+ " order of their height: <br>" );
for (let i = 0; i < n; i++) {
document.write(first_name[i] + " " + last_name[i] + " has height " + height[i] + " cms <br>" );
}
document.write();
} // These arrays together form a set of arrays // known as parallel array let n = 10; let first_name = [ "Bones" , "Welma" ,
"Frank" , "Han" , "Jack" , "Jinny" , "Harry" ,
"Emma" , "Tony" , "Sherlock" ];
let last_name = [ "Smith" , "Seger" ,
"Mathers" , "Solo" , "Jackles" , "Weasly" ,
"Potter" , "Watson" , "Stark" , "Holmes" ];
let height = [169, 158, 201, 183, 172, 152, 160, 163, 173, 185];
// Sorting the above arrays using quickSort // technique based on increasing order of // their heights. quickSort(first_name, last_name, height, 0, n - 1);
printParallelArray(first_name, last_name, height, n);
// Second tallest person in the sorted list // will be the second person from the right end. document.write( "<br><br>Name of the second tallest"
+ " person is " + first_name[n - 2] + " " + last_name[n - 2] + "<br>" );
// Third shortest person in the sorted list // will be the third person from the left end. document.write( "Name of the third shortest"
+ " person is " + first_name[2] + " " + last_name[2] + "<br>" );
// We binary search the height array to // search for a person having height 158 cms. binarySearch(first_name, last_name, height, 158, n); // This code is contributed by gfgking. </script> |
Name of people in increasingorder 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
Advantages:
- 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.
Disadvantages:
- 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.