(Solved):Let x_1, x_2, …, x_n be an array. Consider the following algorithm. for iin1, 2, …, lfloor n/2rfloor do t arrow x_i x_iarrow x_n – i + 1 x_n – i + 1 arrow t (a) How many “arrow” operations d… View Answer…

 

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

Scroll to top