Parallel Arrays



index
Disabled back button Next Section
printable version

Section 1: Introduction

In many problems there are several pieces of information that are related.

For example, if you had name and scores, you might set up a string array for name and a numeric array for scores.

A particular score can be associated with a certain name because they have the same position, and therefore index, in their respective arrays.

Parallel array example.
Section 2: Example

Count the frequency of occurrence of grades in an array of scores.

If you were to do this by hand you would probably make a list of the grades that you want to count.

Such an algorithm can be used directly:

Loop through the grade array (gradeBook) one by one.

for (studentPtr = 0; studentPtr < gradeBook.length; studentPtr++)
{
   var matched = false;
   var gradePtr = 0;

   while ((gradePtr < gradeList.length) && !matched)
   {
      if ( gradeBook[studentPtr] == gradeList[gradePtr] )
      {
         frequency [gradePtr] = frequency [gradePtr] + 1;
         matched = true;
      }
      else
      {
         gradePtr = gradePtr + 1;
      }
   }
}

Parallel array example.
Section 3: Sorting Parallel Arrays

Arrays are normally sorted using the array.sort() function.

One of the simplest types of sorts is the bubblesort.

In the following code, one will be adapted to handle parallel arrays.

//----------------------------------------------------------------------------------
// Compare the last item in the array with the item above it.
// If item n is smaller than item n-1, switch them.
// Compare item n-1 with item n-2.
// If item n-1 is smaller than item n-2, switch them.
// Compare item n-2 with item n-3.
// etc.
//
// Notes:
// 1. Since the largest element is put in its proper position on each pass,
// then the algorithm can consider one fewer elements on subsequent passes.
// In order to accomplish this, one can be added to the loop control variable i,
// and this sum can be used as the final value in a decrementing loop.
// The final value gets larger each pass.
// 2. The boolean "switch" is turned on only if a swap is made, and it stops the
// loop when no more swaps are made, reducing the number of iterations made.
//----------------------------------------------------------------------------------
function bubbleSort (arrayToSort, parallelArray1, order)
{
   var i = 0;
   var moreToSort = true;
   while ((order != "ascending") && (order != "descending"))
   {
      order = prompt("You must specify 'ascending' or 'descending': ", "ascending");
   }

   while ((i <= arrayToSort.length - 2) && moreToSort)
   {
      moreToSort = false;
      for (var j = arrayToSort.length-1; j >= i+1; j--)
      {
         if (order == "ascending")
         {
            if (arrayToSort[j] < arrayToSort[j - 1])
            {
               swap(arrayToSort, j, j - 1);
               swap(parallelArray1, j, j - 1);
               moreToSort = true;
            }
         }
         else // descending
         {
         if (arrayToSort[j] > arrayToSort[j - 1])
         {
            swap(arrayToSort, j, j - 1);
            swap(parallelArray1, j, j - 1);
            moreToSort = true;
         }
      }
   }
   i++;
   }
}

//----------------------------------------------------------------------------------
// Given an array and two indexes, swap the values. Nothing is returned except the modified array
// since arrays are passed by reference.
//----------------------------------------------------------------------------------
function swap ( arrayWithSwaps, one, two )
{
   var temp = arrayWithSwaps[one];
   arrayWithSwaps[one] = arrayWithSwaps[two];
   arrayWithSwaps[two] = temp;
}

If you wanted to sort an additional parallel array you could modify the lines in red:

Parallel Array Demo