Algorithm design and problem-solving (3)

Resources | Revision Questions | Computer Science

Login to see all questions

Click on a question to view the answer

1.

An algorithm is designed to sort a list of numbers in ascending order. You are given the following algorithm:

    Algorithm SortList(list)
    Input: A list of numbers
    Output: The list sorted in ascending order

    1.  Create an empty list called 'sortedList'.
    2.  For each number in 'list':
    3.  If 'sortedList' is empty, add the number to 'sortedList'.
    4.  Otherwise, while 'sortedList' is not empty and the number is less than the first element of 'sortedList':
    5.  Remove the first element of 'sortedList'.
    6.  Add the number to 'sortedList'.
    7.  End
  

a) Explain, in plain English, what the algorithm does. (2 marks)

b) Create a trace table to show how the algorithm would sort the following list: [5, 2, 8, 1, 9]. (4 marks)

IterationlistsortedListExplanation
1[5, 2, 8, 1, 9][]sortedList is empty, so 5 is added.
2[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
3[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
4[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
5[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
6[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
7[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
8[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.
9[5, 2, 8, 1, 9][5]5 is less than the first element (5) of sortedList, so 5 is removed and 5 is added.

c) What is the time complexity of this algorithm in terms of the number of operations it performs? (2 marks)

The worst-case time complexity is O(n2), where n is the number of elements in the list. This is because, in the worst case, for each element in the input list, the algorithm might have to remove elements from the sorted list. The number of removals can be proportional to the number of elements already in the sorted list, leading to a quadratic time complexity.

2.

Question 3

You are tasked with designing a system to manage library books. The system needs to allow users to search for books by title, author, or ISBN, and to track the availability of each book. Describe a solution to this problem, including the data structures and algorithms you would use. Consider how you would handle multiple copies of the same book.

3.

Describe the advantages of using system decomposition and sub-systems when designing a large software project. Provide at least three distinct advantages with brief explanations.