Question bank › IB Computer Science › Theme B – Computational thinking & problem-solving

B1 Computational thinking & algorithms (searching, sorting, Big-O)

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core4 marks

A sorted list is stored as data = [3, 8, 12, 19, 25, 31, 40, 47] (indices 0 to 7). A binary search looks for the value 31. Count each examination of a middle item as one comparison. The midpoint is calculated as mid = (low + high) // 2, starting with low = 0 and high = 7.

(a) State the value of mid and the item examined in the first comparison. [2]

(b) Show how low and high change, and state how many comparisons are needed to find 31. [2]

2Standard4 marks

A bubble sort with a swapped flag is applied to [5, 2, 9, 1, 6]. In each pass adjacent items are compared from left to right and swapped if out of order. The algorithm stops after a pass in which no swap occurs.

(a) Write down the list after the first pass. [1]

(b) State the number of swaps made in the first pass. [1]

(c) Determine the total number of passes the algorithm makes, including the final pass. Show the list after each pass. [2]

3Standard4 marks

State the time complexity in Big-O notation of each code fragment, where n = len(items).

(a)

for i in range(n):
    for j in range(n):
        count += 1

[1]

(b)

i = n
while i > 1:
    i = i // 2

[1]

(c)

total = 0
for x in items:
    total += x

[1]

(d) Outline why the fragment in (b) is much more efficient than the fragment in (c) for very large n. [1]

4Core4 marks

A company is designing a mobile app that shows the quickest bus route between two stops in a city.

(a) Explain how abstraction could be used when designing the app's route map. [2]

(b) Explain how decomposition could be used when developing the app. [2]

5Hard5 marks

A sorted list holds 1000 customer ID numbers.

(a) State the maximum number of comparisons a linear search needs. [1]

(b) Calculate the maximum number of comparisons a binary search needs, counting one comparison for each item examined. Show your working. [2]

(c) The list is replaced by an unsorted list of 1000 IDs. Explain why binary search cannot be used on this list, and why sorting it with selection sort just to carry out one search would not be worthwhile. [2]

6Hard6 marks

Two algorithms check whether a list of n integers contains duplicates. Every integer in the list is between 0 and 99 999.

Algorithm A

def has_dup_a(nums):
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] == nums[j]:
                return True
    return False

Algorithm B

def has_dup_b(nums):
    seen = [False] * 100000
    for x in nums:
        if seen[x]:
            return True
        seen[x] = True
    return False

(a) State the worst-case time complexity of Algorithm A and justify your answer. [2]

(b) Calculate the number of comparisons nums[i] == nums[j] Algorithm A makes in the worst case when n = 10 000. [1]

(c) State the worst-case time complexity of Algorithm B in terms of n. [1]

(d) Describe one disadvantage of Algorithm B compared with Algorithm A. [1]

(e) Identify which algorithm you would recommend for n = 10 000 and give a reason. [1]

Still losing marks on this topic?

Book a free introductory session and we'll find exactly which step is costing you.

Book a free intro