IPA FE Subject B 2023 Question 3
A question IPA Japan published in 令和5年度 基本情報技術者試験 科目B 公開問題 問3, translated into English. ITPEC is based on the same exams.
From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.
The following procedure sort sorts the elements of the global integer-type array data, from the index given by the argument first to the index given by the argument last, in ascending order. Here, first < last. When the procedure sort is called as sort(, the output when the line /*** α ***/ is executed for the first time is “blank”.
Answer group
Explanation
2 1 3 5 4 — the α line first runs at the end of the first partitioning pass of sort(, before either recursive call.
The pivot is data[3] = 3, since the quotient of (1 + 5) ÷ 2 is 3. In the first pass of the outer loop, i moves right past the elements less than 3 and j moves left past the elements greater than 3:
- •
i:data[1]= 2 anddata[2]= 1 are less than 3;data[3]= 3 is not, soistops at 3 - •
j:data[5]= 4 anddata[4]= 5 are greater than 3;data[3]= 3 is not, sojstops at 3
Now i ≥ j, so the loop ends before any swap, and the α line outputs data unchanged: 2 1 3 5 4. Only then do sort( (output 1 2 3 5 4) and sort( (output 1 2 3 4 5) run.
Answer (d)
Why not others:
- •(a) 1 2 3 4 5 — the fully sorted array, printed by the third α line, at the end of
sort(4, 5) - •(b) 1 2 3 5 4 — the second α line's output, after
sort(swaps 2 and 11, 2) - •(c) 2 1 3 4 5 — would need
sort(to have run first; at the first α line neither recursive call has run yet4, 5)
Key rule: In a recursive quicksort, a line after the partition loop but before the recursive calls sees the array after one partition only, and that partition moves elements only by its swaps.
Sorting · 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.