并行阵列: 也称为结构阵列(SoA),多个大小相同的阵列,使得每个阵列的第i个元素密切相关,所有第i个元素一起代表一个对象或实体。平行阵列的一个例子是两个阵列,它们代表n个点的x和y坐标。 下面是另一个例子,我们将5个人的名字、姓氏和身高存储在三个不同的数组中。
null
first_name = ['Bones', 'Welma', 'Frank', 'Han', 'Jack']last_name = ['Smith', 'Seger', 'Mathers', 'Solo', 'Jackles']height = [169, 158, 201, 183, 172]
通过这种方式,我们可以轻松地存储信息,并且为了进行访问,每个数组的第一个索引对应于属于同一个人的数据。 申请: 对数组或记录执行的两个最基本的应用程序是搜索和排序。
- 搜索: 并行数组中的每个索引对应于记录中属于同一实体的数据。因此,为了基于属性的特定值搜索实体,例如,在上述示例中,我们需要找到身高>200厘米的人的姓名。因此,我们在高度数组中搜索值大于200的索引。现在,一旦我们获得了索引,我们就在first_name和last_name数组中打印索引的值。这样,在并行阵列中搜索变得很容易。
- 分类: 现在,使用相同的事实,即每个索引对应于同一实体对应的不同数组中的数据项。因此,我们根据相同的标准对所有数组进行排序。例如,在上面显示的示例中,我们需要按照身高的递增顺序对人进行排序。因此,当我们交换这两个高度时,我们甚至会交换使用相同索引的其他数组中的相应值。
搜索: 执行以下步骤以基于数组或并行数组结构中的特定属性搜索实体。
分类 数组可以以任何方式排序,数字、字母或任何其他方式,但元素放置在等间距的地址。任何排序技术都可以用于根据指定的条件对记录进行排序。执行以下步骤对记录进行排序。
- 在序列无效的数组中找到索引(根据指定标准进行排序的数组)(即,我们需要计算排序序列失败的那两个索引,通过交换这些索引处的值来纠正)。
- 现在交换所有数组中这两个计算索引的值。
因此,按照上述步骤,一旦选定的数组(根据指定标准选择的数组)被排序,所有数组都应根据选定的数组进行排序。 实施: 1) 下面的代码存储了10名学生的名字、第二个名字和身高。 2) 按高度的递增顺序使用 快速排序 算法。 3) 搜索记录中第二高的学生、第三矮的学生和身高等于158厘米的学生的姓名。
C++
// 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" ; } 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
// 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); } } |
C#
// 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 // 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 ] . "" ); 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: " . "" ); for ( $i = 0; $i < $n ; $i ++) { echo ( $first_name [ $i ] . " " . $last_name [ $i ] . " has height " . $height [ $i ] . " cms" ); } echo ( "" ); } // 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] . "" ); // 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] . "" ); // 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) ?> |
Javascript
<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 increasing order of their height:Jinny Weasly has height 152 cmsWelma Seger has height 158 cmsHarry Potter has height 160 cmsEmma Watson has height 163 cmsBones Smith has height 169 cmsJack Jackles has height 172 cmsTony Stark has height 173 cmsHan Solo has height 183 cmsSherlock Holmes has height 185 cmsFrank Mathers has height 201 cmsName of the second tallest person is Sherlock HolmesName of the third shortest person is Harry PotterPerson having height 158 cms is Welma Seger
优势:
- 它们可以用在只支持基元类型数组而不支持记录数组(或者根本不支持记录)的语言中。
- 并行数组易于理解和使用,通常用于声明一个记录比它的价值更麻烦的地方。
- 在某些情况下,通过避免对齐问题,它们可以节省大量空间。
- 如果项的数量很小,那么数组索引占用的空间可能会比完整指针少得多,尤其是在具有大字的体系结构上。
- 在现代机器上,顺序检查数组中每个记录的单个字段非常快,因为这相当于对单个数组的线性遍历,显示了引用和缓存行为的理想局部性。
缺点:
- 当不按顺序访问记录并检查每个记录的多个字段时,它们的引用位置明显较差。
- 它们几乎没有直接的语言支持(该语言及其语法通常不表示并行数组中的数组之间的关系)。
- 由于多个阵列中的每一个都必须重新分配,因此它们的增长或收缩成本都很高。多级阵列可以改善这个问题,但由于需要额外的间接寻址来找到所需的元素,因此会影响性能。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END