0%
0 / 15 answered

Implementing Selection and Iteration Algorithms Practice Test

15 Questions
Question
1 / 15
Q1

Given the task requirements, what will be the output after executing the provided code snippet?

You are implementing selection sort. After each outer-loop pass, the smallest remaining element is swapped into position i. Use iteration for scanning and selection for tracking the minimum.

Input: $n$, then $n$ integers.

Output: sorted array.

Constraints: $1 \le n \le 100$; values in -1000,1000.

Example input:

4

5 2 9 1

Example output:

1 2 5 9

Code snippet:


int[] a = {5, 2, 9, 1};

**for** (int i = 0; i < a.length - 1; i++) {

    int min = i;

    **for** (int j = i + 1; j < a.length; j++) {

        **if** (a<u>j</u> < a<u>min</u>) {

            min = j;

        }

    }

    int tmp = a<u>i</u>;

    a<u>i</u> = a<u>min</u>;

    a<u>min</u> = tmp;

}

System.out.print(a<u>0</u> + " " + a<u>1</u>);

Question Navigator