Note AcademyNote Academy

Computer Science · Oct / Nov 2024 · Paper 22 · Question 4

0478

As printed in the paper

The question as text

This pseudocode algorithm is intended to sort a pre-populated one-dimensional (1D) array named Šx¦sy§x into alphabetical order using a bubble sort. ¨‚ †‡qsrˆ‡ Šx¦sy§x ‰ rˆˆr“©‚‰‚¨¨ª ˜Ž uŒˆŠ‹ ¨„ †‡qsrˆ‡ qv€zxw ‰ uŒˆŠ‹ ¨« †‡qsrˆ‡ sy¦yx ‰ Š‹Œ‡‡ˆ ¨¬ †‡qsrˆ‡ ­~§§ ‰ Š‹Œ‡‡ˆ ¨® †‡qsrˆ‡ u¯~°°± ‰ ²˜˜s‡r‹ ¨³ †‡qsrˆ‡ Œ¦° ‰ uŒˆŠ‹ ¨‘ sy¦yx ‚¨¨ ¨´ ­~§§ ‚ ¨µ Œ¦° Œˆ¶‡ ‚¨ ”’Šs‡ u¯~°°±  Œˆ¶‡ ˜ˆ ­~§§ • sy¦yx — ‚ †˜ ‚‚ u¯~°°± Žrsu‡ ‚„ Ž˜ˆ qv€zxw ‚ Œ˜ sy¦yx · ­~§§ ‚« ŠŽ Šx¦sy§x©qv€zxwª – Šx¦sy§x©qv€zxw ¸ ‚ª ‚¬ Œ’‡‹ ‚® Œ¦° Šx¦sy§x©qv€zxwª ‚³ Šx¦sy§x©qv€zxwª Šx¦sy§x©qv€zxw ¸ ‚ª ‚‘ Šx¦sy§x©qv€zxwª Œ¦° ‚´ u¯~°°± Œˆ¶‡ ‚µ ‡‹†qru‡ „¨ ­~§§ ­~§§ ¸ ‚ „‚ ‹‡Œ qv€zxw „„ ‡‹†”’Šs‡

Read automatically from the paper; the printed page above is exact.

Show the answer and mark scheme
  • 4(a)[5]

    One mark per mark point

    Line 02 / DECLARE Counter : STRING

    should be DECLARE Counter : INTEGER

    Line 09 / Temp ← TRUE

    should be Swapped ← TRUE

    Line 10 / WHILE Swapped = TRUE OR Pass <= Limit - 1 DO should be WHILE Swapped = TRUE AND Pass <= Limit - 1 DO
    Line 17 / ItemList[Counter] ← Temp

    should be ItemList[Counter + 1] ← Temp

    Line 19 / ENDCASE

    should be ENDIF

    Correct algorithm:

    01 DECLARE ItemList : ARRAY[1:100] OF STRING

    02 DECLARE Counter : INTEGER

    03 DECLARE Limit : INTEGER

    04 DECLARE Pass : INTEGER

    05 DECLARE Swapped : BOOLEAN

    06 DECLARE Temp : STRING

    07 Limit ← 100

    08 Pass ← 1

    09 Swapped ← TRUE

    10 WHILE Swapped = TRUE AND Pass <= Limit - 1 DO

    11 Swapped ← FALSE

    12 FOR Counter ← 1 TO Limit – Pass

    13 IF ItemList[Counter] > ItemList[Counter + 1] 14 THEN

    15 Temp ← ItemList[Counter]

    16 ItemList[Counter] ← ItemList[Counter + 1] 17 ItemList[Counter + 1] ← Temp

    18 Swapped ← TRUE

    19 ENDIF

    20 Pass ← Pass + 1

    21 NEXT Counter

    22 ENDWHILE

  • 4(b)[3]

    One mark per mark point (max three)

    The use of a flag (set initially to FALSE) to show if a swap has been made (during the current iteration)
    … to stop the loop if it has been sorted
    The reduction in the limit of the (inner) loop after each iteration (of the loop)
    … to reduce the number of comparisons / iterations required

Make a free account to save your answers and see your weak topics and ask the AI why a wrong answer is wrong.