Programming Basics (Pseudocode Tracing)

The following procedure bubble is a bubble sort that sorts the integer array data in ascending order. swap(data, j, k) is a procedure that swaps the values of data[j] and data[k]. Array indices start at 1. What should replace 【a】 in the program?

○bubble(integer array: data)
  integer: n ← length(data)
  for (i from n down to 2 step 1)
    for (j from 1 to 【a】 step 1)
      if (data[j] > data[j + 1])
        swap(data, j, j + 1)
      endif
    endfor
  endfor
  1. Ai
  2. Bn − i
  3. Ci − 1
Show answer and explanation

Answer: C. i − 1

The outer i is the position to which each pass moves the largest remaining value (n, n − 1, …, 2 from the end). The inner loop compares data[j] with data[j + 1], so a final value of i − 1 for j compares adjacent pairs up to data[i] without going beyond the array. The first pass, with i = n, moves the largest value to data[n]; the next goes up to data[n − 1], and so on, narrowing the range by one each time. With i in 【a】, the first pass would read data[n + 1].

  • A. For i = n, j reaches n and data[n + 1] is read, beyond the end of the array.
  • B. For i = n the final value of j is 0 and nothing is compared; the ranges are reversed and the data is not sorted. {5, 3, 8, 1, 4} becomes {3, 5, 1, 8, 4}.
  • C. Correct. Adjacent pairs up to data[i] are compared, moving the largest value to data[i].
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. The following program finds in lo the index of the first value that is at least x in the integer array data, which is sorted in ascending order (indices start at 1). x is no greater than the last element of data. What should replace 【a】 in the program?
  2. A stack is represented by the 5-element integer array st and the integer variable top. All elements of st start at 0 and top starts at 0. push(v) adds 1 to top and then does st[top] ← v; pop() returns the value of st[top] and then subtracts 1 from top (the elements of st are not changed). What are top and st after the following program runs?
  3. A queue uses the 4-element string array q as a ring. head and tail start at 1. enq(v) does q[tail] ← v and then tail ← tail mod 4 + 1; deq() returns the value of q[head] and then does head ← head mod 4 + 1. When the following program runs, in which element of q is "E" stored?
  4. The following program outputs a linked list, whose elements are linked by the array next, by following it from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What sequence of values is output?
  5. The following program inserts "F", in val[6], right after "C" (index 3) in the linked list A → C → E → D → B, linked by the array next, and then outputs the list from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What should replace 【a】 so that the output is A, C, F, E, D, B?