IPA IP 2025 Question 98

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

When a sequence of data consisting of four (4) elements is sorted in ascending order by repeating the procedure below, how many times is the series of steps (1) through (3) executed until the sorting is completed? Here, at first, the whole sequence of data is the target of sorting.

Sequence of data: [27, 42, 33, 12]

[Procedure]

(1)Select the element with the maximum value among the elements in the sorting target, and swap it with the last element.(2)Exclude the last element from the sorting target.(3)If one (1) or more elements remain in the sorting target, execute the series of steps (1) through (3). If none remain, sorting is completed, so the procedure ends.

Explanation

4 times — each run removes one element from the sorting target, and the procedure repeats while at least one element is left.

Tracing [27, 42, 33, 12]:

RunTarget beforeMaximumAfter (1)Target left
127, 42, 33, 124227, 12, 33, 423
227, 12, 333327, 12, 332
327, 122712, 271
41212120

After run 3 one element (12) remains, so step (3) starts run 4; only after it is the target empty. A run of the procedure for the list confirms 4.

Answer (c)

Why not others:

  • •(b) 3 — stops when the data is already in order after run 3, but step (3) repeats while one or more elements remain
  • •(a) 2 — counts only the runs in which a swap changed the order (runs 1 and 3); runs 2 and 4 are executed too, with the maximum already last
  • •(d) 5 — one run more than there are elements

Key rule: In a selection-type sort, count runs from the loop condition, not from when the data looks sorted: here one run per element, n runs for n elements.

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.