Implementation of Parallel Arrays

In this article, we will learn what parallel arrays are and how to implement them.

Let’s say you want to store some details about employees working in a company. There is a total of 9 employees who have ids from 0-9. The information includes employee name, salary, and years of experience. Now, you can save one of the details, let’s say the name, in a single array. But what about the other information? To solve that problem, you can make use of parallel arrays.

What are Parallel Arrays?

Parallel arrays are two or more arrays that store information about an object or an entity such that the items at the index i correspond to a single object.

For the employees’ example, we will have three arrays to store an employee’s name, salary, and years of experience. Elements at index 0 in each array will have details of the employee with id 0, index 1 with id 1, etc.

Parallel arrays are useful when the programming language does not support objects, structs, or arrays of objects. Moreover, they occupy less space than objects.

Implementation of Parallel Arrays

#rom-com movies and their imdb ratings
movies = ["Juno", "Love Actually", "Before Sunrise", "500 Days of Summer", "Friends with Benefits"]
imdb_ratings=[7.4, 7.6, 8.1, 7.7, 6.5]

for movie, rating in zip(movies, imdb_ratings):
  print(f"Movie: {movie} has Rating: {rating}")

Output

Movie: Juno has Rating: 7.4
Movie: Love Actually has Rating: 7.6
Movie: Before Sunrise has Rating: 8.1
Movie: 500 Days of Summer has Rating: 7.7
Movie: Friends with Benefits has Rating: 6.5

In the above example, we have created parallel arrays to store the movie names and their IMDb ratings. Then, we traverse them in the for loop using zip(). The zip() method takes movies and imdb_ratings and returns a tuple iterator. As you notice in the example, we have used lists instead of arrays. For this article, they will do fine as they will make our code simple. Moreover, arrays and lists terms are used interchangeably here.

Insertion, Deletion, and Update

Operations like insertion and deletion are quite simple and straightforward. When performing any such operation, we have to make sure that the changes are made at the right and the same index in each array.

#add an item in parallel array
movies.append("Set it Up")
imdb_ratings.append(6.5)

#delete an item
movies.pop(0)
imdb_ratings.pop(0)

#update the rating of the second movie
imdb_ratings[1]=8

for movie, rating in zip(movies, imdb_ratings):
  print(f"Movie: {movie} has Rating: {rating}")

Output

Movie: Love Actually has Rating: 7.6
Movie: Before Sunrise has Rating: 8
Movie: 500 Days of Summer has Rating: 7.7
Movie: Friends with Benefits has Rating: 6.5
Movie: Set it Up has Rating: 6.5

Sort

Let’s now see how to sort arrays by IMDb ratings. Here, we need to make sure that whenever any elements get swapped while sorting the imdb_ratings array, the corresponding changes are also made in the movies array.

def merge(arr1, arr2,  l, r, m): #arr1 is the movies array and arr2 is the imdb_ratings array
  left_arr1 = arr1[l:m+1] #slicing the left part 
  right_arr1 = arr1[m+1:r+1] #slicing the right part
  left_arr2 = arr2[l:m+1]
  right_arr2 = arr2[m+1:r+1]
  i=0 # index for the left part
  j=0 #index for thr right part
  k=l #index for the main array
  while (i<len(left_arr2) and j<len(right_arr2)):
    if left_arr2[i]<=right_arr2[j]:
      arr2[k] = left_arr2[i]
      arr1[k] = left_arr1[i]
      i=i+1
    else:
      arr2[k] = right_arr2[j]
      arr1[k] = right_arr1[j]
      j=j+1
    k=k+1
  while i<len(left_arr2):
    arr2[k] = left_arr2[i]
    arr1[k] = left_arr1[i]
    i = i+1
    k=k+1
  while j<len(right_arr2):
    arr2[k] = right_arr2[j]
    arr1[k] = right_arr1[j]
    j = j+1
    k=k+1
  

#sorting based on imdb_ratings
def mergeSort(arr1, arr2, l, r):
  if (l>= r):
    return;
  m = (l+r)//2
  mergeSort(arr1, arr2, l, m)
  mergeSort(arr1, arr2, m+1, r)
  merge(arr1, arr2, l, r, m)

In the above example, we have the code to apply the merge sort algorithm. Here, arr1 represents the movies array, and arr2 represents the imdb_ratings array. As you can observe in the merge() function, the sorting conditions include arr2 only. However, the swapping is done in both arrays.

#rom-com movies and their imdb ratings
movies = ["Juno", "Love Actually", "Before Sunrise", "500 Days of Summer", "Friends with Benefits"]
imdb_ratings=[7.4, 7.6, 8.1, 7.7, 6.5]

print("Result after sorting")
mergeSort(movies, imdb_ratings, 0, len(imdb_ratings)-1)
for movie, rating in zip(movies, imdb_ratings):
  print(f"Movie: {movie} has Rating: {rating}")

Output

Result after sorting
Movie: Friends with Benefits has Rating: 6.5
Movie: Juno has Rating: 7.4
Movie: Love Actually has Rating: 7.6
Movie: 500 Days of Summer has Rating: 7.7
Movie: Before Sunrise has Rating: 8.1

The above output shows sorted parallel arrays.

Search

Let’s now search those movies having ratings greater than or equal to 7.

#movies having ratings greater than or equal to 7
indices = [index for index, rating in enumerate(imdb_ratings) if rating>=7]
for index in indices:
  print(f"Movie: {movies[index]} Ratings: {imdb_ratings[index]}")

Output

Movie: Juno Ratings: 7.4
Movie: Love Actually Ratings: 7.6
Movie: Before Sunrise Ratings: 8.1
Movie: 500 Days of Summer Ratings: 7.7

First, we find indices of items that satisfy the specified condition, and then, we display only those entities.

 

Back to: Data Structure > Data Structure - Arrays

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.