IPA IP 2026 Question 67

A question IPA Japan published in 令和8年度 ITパスポート試験 公開問題 問67, translated into English. ITPEC is based on the same exams.

The procedure sort receives an integer-type array with 2 or more elements as the argument numberArray, and outputs the result of sorting its elements in ascending order. To check the operation of the procedure sort, the value of j and all the elements of workArray are output in the middle of the processing. When the array numberArray is {3, 5, 1, 2, 4} and the procedure sort is called as sort(numberArray), which of the following is the output of all the elements of workArray immediately after the value of j is output as 3? Here, the array index starts at 1.

[Program]
○ sort(integer []: numberArray)
integer: minIndex, j, k
integer []: workArray ← numberArray // Make a copy of the array
for (increase j from 1 to (the number of elements in workArray − 1) by 1)
// Find the index of one of the elements that have the smallest value among
// the elements from the j-th element to the last element
minIndex ← j
for (increase k from (j + 1) to the number of elements in workArray by 1)
if (workArray[k] is less than workArray[minIndex])
minIndex ← k
endif
endfor
swap the values of workArray[j] and workArray[minIndex]
// To check the operation, output the value of j and all the elements of workArray
output the value of j
output all the elements of workArray from the first one in order, separated by commas
endfor
output all the elements of workArray from the first one in order, separated by commas

Explanation

1,2,3,5,4 — the program is a selection sort, and after three passes only the first three elements are in their final places.

Each pass j finds the smallest value from position j to the end and swaps it into position j, then outputs j and the array.

PassSmallest of positions j to 5SwapworkArray after the pass
j = 11 at position 3positions 1 and 31,5,3,2,4
j = 22 at position 4positions 2 and 41,2,3,5,4
j = 33 at position 3itself, no change1,2,3,5,4

Right after 3 is output, the array is 1,2,3,5,4. The 5 and 4 are swapped only in pass 4.

Answer (b)

Why not others:

  • •(a) 1,2,3,4,5 — the fully sorted array, output after pass 4 (and at the end), not after pass 3
  • •(c) 4,5,3,2,1 and (d) 5,4,3,2,1 — descending orders; the program swaps the smallest value forward, so the front of the array is always ascending

Key rule: In a selection sort, after pass j the first j positions hold the j smallest values in order; the rest is unsorted until its own pass.

Algorithm and Programming · AI-written, may contain errors

This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.