Question

Let {eq}displaystyle x_1, x_2, …, x_n

{/eq} be an array.

Consider the following algorithm.

{eq}text{for } iin {1, 2, …, leftlfloor n/2rightrfloor} text{ do }\

t leftarrow x_i\

x_ileftarrow x_{n – i + 1}\

x_{n – i + 1}leftarrow t

{/eq}

(a) How many {eq}”leftarrow”

{/eq} operations does this algorithm perform?

(b) What does this algorithm do to the array?

EXPERT ANSWER

(a)The given algorithm has a **for** loop consisting of {eq}3

{/eq} {eq}”leftarrow”

{/eq} operations in it’s body. So, through each iteration