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.
fori {1, 2, ... , n/2}
dot 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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.