1. This question is on program tracing and related issues. Consider the followin
ID: 3688314 • Letter: 1
Question
1. This question is on program tracing and related issues. Consider the following algorithm which is supposed to begin its operation with an array a of length n > 0 which stores n integer values. int temp; for (int i = n-1; i > 0; i--) if (a[i] < a[i-1]) { // swap values of a[i] and a[i-1] temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; } 1 2 3 4 5 6 7 8 (a) Suppose that the array a initially stores the values {5, 7, 2, 9}. What values are stored in the array a after the above algorithm has been executed? (b) Next suppose that the array a initially stores n > 0 arbitrary integer values. Which of the following alternatives best describes an effect which we can be sure that the above algorithm achieves after it has been executed? A. a[0] stores the smallest element of the array a. B. a[0] stores the largest element of the array a. C. a[n-1] stores the smallest element of the array a. D. a[n-1] stores the largest element of the array a. E. the n elements of a are in ascending order. Hint. If not sure, try tracing the algorithm for another set of initial values, such as {3, 2, 1}.Explanation / Answer
a) values stored in the array a after the above algorithm has been executed: 2 5 7 9
because this algo sorts the number in increasing order
b)Correct alternatives are
A. a[0] stores the smallest element of the array a.
D. a[n-1] stores the largest element of the array a.
E. the n elements of a are in ascending order.
As, the algo is to sort the array elements in increasing order, the forst element will be the smallest element of all
and last a[n-1] will the largest of all
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.