The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+8 votes

The below question is based on the following program.

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

When the procedure terminates, the array A has been:

  1. Reversed
  2. Sorted in descending order
  3. Left unaltered
  4. Sorted in ascending order
in Algorithms by Veteran (98.3k points)
edited by | 265 views
@kenzou, It would be better to shift the "endfor" stmt to align with "for".  May get confused as break in the pseudocode.
This code is finding the position of max element in the unsorted part of the array and interchanging it with the first element in the unsorted part.

2 Answers

+9 votes
Best answer
Answer is $B$. Sorted in descending order ( selection sorting algorithm is used ).
by Boss (26.2k points)
edited by
+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
by Active (2.3k 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
49,807 questions
54,713 answers
79,710 users