The Gateway to Computer Science Excellence
+2 votes
75 views

The next two questions refer to the following program.

In the code below reverse$(A,i,j)$ takes an array $A,$ indices $i$ and $j$ with $i\leq j,$ and reverses the segment $A[i],A[i+1],\cdots,A[j].$ For instance if $A=[0,1,2,3,4,5,6,7]$ then, after we apply reverse$(A,3,6),$ the contents of the array will be $A=[0,1,2,6,5,4,3,7].$

function mystery (A[0...99]) {
  int i, j, m;
  for (i = 0; i < 100; i++) {
    m = i;
  for (j = i; j < 100; j++) {
    if (A[j] > A[m]) {
        m = j;
       }
     }
     reverse(A,i,m);
   }
   return;
 }

When the procedure terminates, the array A has been:

  1. Sorted in descending order
  2. Sorted in ascending order
  3. Reversed
  4. Left unaltered
in Programming by Boss (16.8k points)
recategorized by | 75 views
0
Where are the next two questions??

1 Answer

0 votes

 

Just take a simple example as : - arr : 3 5 1 2 4 6 

 For outer loop when i=0,m=0 and now internal loop execute from j=i to length of arr and check for each element greatest element in the array form j=i to length of array, and save its index in the variable m. Now reverse(A,0,m) will just reverse the array from the index i to m so the greatest element will now become the first element and 

So on :

 

Pic1 

 

Caption

 

by (165 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,492 answers
195,456 comments
100,758 users