Sorting
Sorting
(ii) A special case is when NameList is already in order. The algorithm in part (a)(i) is
applied to this special case.
Explain how many iterations are carried out for each of the loops.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [3]
FOR ThisPointer ← 1 TO 9
FOR Pointer ← 1 TO 9
IF NameList[Pointer] > NameList[Pointer + 1]
THEN
Temp ← NameList[Pointer]
NameList[Pointer] ← NameList[Pointer + 1]
NameList[Pointer + 1] ← Temp
ENDIF
ENDFOR
ENDFOR
(i) As in part (a)(ii), a special case is when NameList is already in order. The algorithm in
part (b) is applied to this special case.
Explain how many iterations are carried out for each of the loops.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [2]
(ii) Rewrite the algorithm in part (b), using pseudocode, to reduce the number of
unnecessary comparisons. Use the same variable names where appropriate.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [5]
PollData 12 85 52 57 25 11 33 59 56 91
CardData 11 12 25 33 52 56 57 59 91 85
(a) State why it will take less time to complete an insertion sort on CardData than on PollData.
...................................................................................................................................................
...............................................................................................................................................[1]
(b) The following pseudocode algorithm performs an insertion sort on the CardData array.
01 ArraySize ← 10
03 ValueToInsert ← CardData[Pointer]
04 HolePosition ← .............................................................................................................
07 HolePosition ← .........................................................................................................
08 ENDWHILE
09 CardData[HolePosition] ← ........................................................................................
10 ENDFOR
[7]
(c) (i) A binary search algorithm is used to find a specific value in an array.
Explain why an array needs to be sorted before a binary search algorithm can be used.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[2]
11 12 25 33 52 56 57 59 85 91
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[4]
(d) Complete this procedure to carry out a binary search on the array shown in part (c)(ii).
First ← 1
Found ← FALSE
Midpoint ← .............................................................................................................
IF CardData[Midpoint] = SearchValue
THEN
Found ← TRUE
ELSE
THEN
Last ← ...........................................................................................
ELSE
First ← .........................................................................................
ENDIF
ENDIF
ENDWHILE
ENDPROCEDURE
[4]
5 The following procedure performs an insertion sort on the global array TheArray that has 10
elements.
PROCEDURE InsertionSort()
Count ..............................................
Temp TheArray[Count]
TheArray[Counter + 1] TheArray[Counter]
Counter Counter - 1
ENDWHILE
TheArray[..............................................] Temp
Count Count + 1
ENDWHILE
ENDPROCEDURE
[5]
4 (a) The array Numbers[0 : Max] stores numbers. An insertion sort can be used to sort these
numbers into ascending order.
ItemToInsert .......................................................................................
CurrentItem ........................................................................................
Numbers[........................................................] Numbers[CurrentItem - 1]
CurrentItem CurrentItem – 1
ENDWHILE
Numbers[CurrentItem] ...........................................................................
ENDFOR
[4]
(b) Identify two features of the array Numbers that would have an impact on the performance of
this insertion sort algorithm.
1 ................................................................................................................................................
2 ................................................................................................................................................
[2]
3 A bubble sort algorithm is used to sort an integer array, List. This algorithm can process arrays
of different lengths.
04 THEN
05 Temp ..................................................................
06 List[Inner] ....................................................................
07 List[Inner + 1] .........................................................................
08 ENDIF
09 ENDFOR
10 ENDFOR
[7]
...................................................................................................................................... [1]
(ii) State which line of the algorithm you would change to sort the array into the opposite
order.
Line ...................................................................................................................................
Change ..............................................................................................................................
...........................................................................................................................................
[1]
(c) Use pseudocode to write an alternative version of this bubble sort algorithm that will exit the
algorithm when the list is fully sorted.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [4]
1 ................................................................................................................................................
2 ................................................................................................................................................
[2]
LowerBound 0
UpperBound LengthOfList – 1
ValueFound FALSE
OUTPUT "Value to find: "
INPUT ValueToFind
WHILE ValueFound = FALSE AND UpperBound <> LowerBound
MidPoint (LowerBound + UpperBound)DIV 2
IF List[MidPoint] = ValueToFind
THEN
ValueFound TRUE
ELSE
IF List[MidPoint] < ValueToFind
THEN
LowerBound MidPoint + 1
ELSE
UpperBound MidPoint – 1
ENDIF
ENDIF
ENDWHILE
IF ValueFound = FALSE
THEN
MidPoint (LowerBound + UpperBound) DIV 2
IF List[MidPoint] = ValueToFind
THEN
OUTPUT "Item in position " & MidPoint & " in list"
ELSE
OUTPUT "Not in list"
ENDIF
ELSE
OUTPUT "Item in position " & MidPoint & " in list"
ENDIF
(i) Complete the trace table to show a dry run of the algorithm, when the value 21 is input.
[3]
..................................................................................................................................... [1]
..................................................................................................................................... [1]
(iv) State the minimum number of times the while loop condition will be executed to search
for a value.
..................................................................................................................................... [1]
(v) MidPoint is calculated and checked again after the while loop is terminated.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
Explain why the algorithm will not find the value 2 in this data set.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
1 An array, NumberArray, stores 100 integer values. The array needs to be sorted into ascending
numerical order.
(a) Describe how an insertion sort will sort the data in NumberArray.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
The procedure Bubble() takes an array as a parameter. It performs a bubble sort on the
array. The sorting algorithm stops as soon as all the elements are in ascending order.
Outer LENGTH(NumberArray) - 1
REPEAT
Inner ...............................................
Swap FALSE
REPEAT
THEN
Temp NumberArray[Inner]
NumberArray[Inner] NumberArray[Inner + 1]
NumberArray[Inner + 1] Temp
Swap ...............................................
ENDIF
Inner Inner + 1
Outer Outer - 1
ENDPROCEDURE
[5]