@kenzou, It would be better to shift the "endfor" stmt to align with "for". May get confused as break in the pseudocode.

The Gateway to Computer Science Excellence

+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:

- Reversed
- Sorted in descending order
- Left unaltered
- Sorted in ascending order

+11 votes

+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

52,375 questions

60,574 answers

201,979 comments

95,388 users