67.2k views
0 votes
Complete the pseudo code below for the algorithm `insert(a, n, i)`. Given an array (a) with (n) items and (a0..i-1) sorted from min to max, insert (ai) to the correct place in (a0..i-1) such that (a0..i) is sorted. After calling `insert(a, 8, 6)`, how will array (a) look?

a) (3, 5, 8, 10, 24, 56, 89, 1)

b) (1, 3, 5, 8, 10, 24, 56, 89)

c) (89, 56, 24, 10, 8, 5, 3, 1)

d) (10, 8, 5, 3, 1, 24, 56, 89)

1 Answer

5 votes

Final answer:

The algorithm insert(a, n, i) is used to insert an element into a sorted array at the correct position so that the resulting array is still sorted. After calling insert(a, 8, 6) with the given array (3, 5, 8, 10, 24, 56, 89, 1), the resulting array will be (1, 3, 5, 8, 10, 24, 56, 89).

Step-by-step explanation:

The algorithm insert(a, n, i) is used to insert an element into a sorted array at the correct position so that the resulting array is still sorted. Here is the pseudo code for the algorithm:

insert(a, n, i):
temp = a[i]
j = i-1
while j >= 0 and a[j] > temp:
a[j+1] = a[j]
j = j-1
a[j+1] = temp

After calling insert(a, 8, 6) with the given array (3, 5, 8, 10, 24, 56, 89, 1), the resulting array will be (1, 3, 5, 8, 10, 24, 56, 89).

User Rickard Nilsson
by
8.4k points