• edited by
1,744 views
9 9 votes

The below question is based on the following program.

procedure mystery (A : array [1..100] of int)
    int i,j,position,tmp;
    begin
        for j := 1 to 100 do
            position := j;
            for i := j to 100 do
                if (A[i] > A[position]) then
                    position := i;
                endfor
            tmp := A[j];
            A[j] := A[position];
            A[position] := tmp;
        endfor
end

When the procedure terminates, the array A has been:

  1. Reversed
  2. Left unaltered
  3. Sorted in descending order
  4. Sorted in ascending order

3 Answers

Best answer
12 12 votes
Answer is $B$. Sorted in descending order ( selection sorting algorithm is used ).
• edited by
2 2 votes
void selectionSort(int arr[], int n)
{
    int i, j, min_idx;
 
    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min_idx])
            min_idx = j;
 
        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
    }
}
 
 
 
Selection sort arranges elements in descending order.
Answer : B
Position:
Show:

Related questions

6 6 votes
3 answers 3 answers
2.1k
2.1k views
go_editor asked May 23, 2016
2,113 views
The below question is based on following program:procedure mystery (A : array [1..100] of int) int i,j,position,tmp; begin for j := 1 to 100 do position := j; for i := j ...
33 33 votes
5 answers 5 answers
10.5k
10.5k views
go_editor asked May 23, 2016
10,505 views
You have $n$ lists, each consisting of $m$ integers sorted in ascending order. Merging these lists into a single sorted list will take time:$O(nm \log m)$$O(mn \log n)$...
2 2 votes
3 3 answers
2.8k
2.8k views
Arjun asked Jun 8, 2016
2,789 views
Your final exams are over and you are catching up on watching sports on TV. You have a schedule of interesting matches coming up all over the world during the next week. ...
1 1 vote
2 2 answers
1.4k
1.4k views
go_editor asked May 23, 2016
1,433 views
Your final exams are over and you are catching up on watching sports on TV. You have a schedule of interesting matches coming up all over the world during the next week. ...