Question: Consider the following algorithm. // Assume that n is a positive integer (= i.e. n > 1), and A[1..n] is a global array . //

Consider the following algorithm.

// Assume that n is a positive integer (= i.e. n > 1), and A[1..n] is a global array.

// Note that the index of array A starts from one, not zero.

// And also, dont forget the array A in the algorithm is global.

Algorithm DoSomething (n)

1. if (n = 1)

2. print the current content of the whole array A on the screen;

3. else

4. for i <-- 1 to n do

5. DoSomething(n 1); // Recursive call.

6. if n is odd

7. swap A[1] and A[n];

8. else

9. swap A[i] and A[n];

10. return;

(a) Present execution result of the algorithm where an array A has 5and n is 1.

(b) Present execution result of the algorithm where an array A has 5, 7and n is 2.

(c) Present execution result of the algorithm where an array A has 5, 7, 9and n is 3.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!