Sorting Algorithms Question Paper
Sorting Algorithms Question Paper
sorting algorithms
Class: ________________________
Date: ________________________
Time: 49 minutes
Marks: 44 marks
Comments:
Page 1 of 7
Q1.
The algorithm below is a sorting algorithm.
1 arr ← [4, 1, 6]
2 swapsMade ← false
3 WHILE swapsMade = false
4 swapsMade ← true
5 i ← 0
6 WHILE i < 2
7 IF arr[i+1] < arr[i] THEN
8 t ← arr[i]
9 arr[i] ← arr[i+1]
10 arr[i+1] ← t
11 swapsMade ← false
12 ENDIF
13 i ← i + 1
14 ENDWHILE
15 ENDWHILE
(a) State the data type of the variable swapsMade in the algorithm shown above.
___________________________________________________________________
(1)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(c) Shade one lozenge to show which of the following contains the false statement
about the algorithm.
(1)
(d) Complete the trace table for the algorithm shown above. Some values have already
been entered.
Page 2 of 7
arr
swawpsMade i t
[0] [1] [2]
4 1 6 false
(6)
(Total 10 marks)
Q2.
Fill in the blank arrays to show the steps involved in applying the bubble sort algorithm to
the array [3, 5, 1, 4, 2]. You only need to show the missing steps where a change is
applied to the array.
3 5 1 4 2
1 2 3 4 5
(Total 5 marks)
Q3.
The algorithm below is a sorting algorithm.
Page 3 of 7
1 arr ← [4, 1, 6]
2 sorted ← false
3 WHILE sorted = false
4 sorted ← true
5 i ← 0
6 WHILE i < 2
7 IF arr[i+1] < arr[i] THEN
8 t ← arr[i]
9 arr[i] ← arr[i+1]
10 arr[i+1] ← t
11 sorted ← false
12 ENDIF
13 i ← i + 1
14 ENDWHILE
15 ENDWHILE
(a) State the data type of the variable sorted in the algorithm shown above.
___________________________________________________________________
(1)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(c) Shade one lozenge to show which of the following contains the false statement
about the algorithm above.
(d) Complete the trace table for the algorithm shown above. Some values have already
been entered.
arr
sorted i t
[0] [1] [2]
4 1 6 false
Page 4 of 7
(6)
(e) Fill in the values in the boxes to show how the merge part of the merge sort
algorithm operates. The first and last rows have been completed for you.
(3)
(f) State one advantage of the merge sort algorithm compared to the sorting algorithm
in Figure 2.
___________________________________________________________________
___________________________________________________________________
(1)
State two reasons why the programmer decided to implement the algorithm as a
subroutine.
Reason 1 ___________________________________________________________
___________________________________________________________________
Page 5 of 7
Reason 2 ___________________________________________________________
___________________________________________________________________
(2)
(Total 16 marks)
Q4.
The image below shows a merge sort being carried out on a list.
The sort is carried out in two phases. The phases are separated by the dotted line.
(a) Explain the process that is being carried out in Phase 1 of the sort (above the dotted
line).
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
(b) Explain the process that is being carried out in Phase 2 of the sort (below the dotted
line).
___________________________________________________________________
___________________________________________________________________
Page 6 of 7
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
(Total 6 marks)
Q5.
State one advantage and one disadvantage of merge sort when compared to bubble sort.
Advantage: _____________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Disadvantage: ___________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 2 marks)
Q6.
Fill in the blank arrays to show the steps involved in applying the bubble sort algorithm to
this array [3, 5, 1, 4, 2] . You need only show the missing steps where a change is
applied to the array:
3 5 1 4 2
1 2 3 4 5
(Total 5 marks)
Page 7 of 7