# 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)