Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let x 1 , x 2 , ... , x n be an array. Consider the following algorithm. t x i x

ID: 3726377 • Letter: L

Question

Let

x1, x2, ... , xn

be an array. Consider the following algorithm.

t xi

xi xn i + 1

xn i + 1 t

(a) How many "" operations does this algorithm perform?

3n n/2     3n/2 n 3 · n/2


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

It adds 1 to each element of the array.

It shifts the first element to the end of the array and every other element one place to the left.    

It shifts the last element to the beginning of the array and every other element one place to the right.

It reverses the order of the elements in the array.

It subtracts 1 from each element of the array.

for

i {1, 2, ... , n/2}

do     

t xi

    

xi xn i + 1

xn i + 1 t

Explanation / Answer

a)

The number of assignment operations performed will be 3* n/2

b)

It reverses the order of the elements in the array. as we are just swapping the corresponding elements from starting and end of the array

1 with n

2 with n-1

and so on

Do give a thumbs up and in case there are doubts leave a comment