You are on page 1of 8

Searching and sorting algorithms in OCR Exam Reference Language

Linear search Linear searches work on ordered and unordered arrays!

How it could look in an exam Explanation


//Function is defined called LinearSearch In this example a function is created that takes two parameters
01 function LinearSearch(searchList, searchItem)  An array of items to be searched through: searchList
02 searchIndex = 0  The item to be searched for: searchItem
//Loop through each index in the array searchList
03 while searchIndex < len(searchList)
How does it work?
 Line 01: The function is defined with two parameters
//If the item in the searchList equals searchItem
 Line 02: A loop counter searchIndex is set to 0
//then we return true
 Line 03: The WHILE loop starts at index 0, and repeats until the end of the
04 if searchList[searchIndex] == searchItem then
list.
05 return true
 Using len(searchList) allows any array size to be used.
06 endif
//If a match is not made, increment the searchIndex  Line 04: Compares the element in the array with the item we are searching
07 searchIndex = searchIndex + 1 for.
08 endwhile  If the element matches the searchItem then true is returned.
 Because a return is used here, the function then terminates by default.
//If no match is found, then we return false  Line 07: If there is no match, then searchIndex increments by 1.
09 return false  The WHILE loop iterates (back to Line 03) using the new
10 endfunction searchIndex value.
 The WHILE loop ceases to run if the end of the array is reached.
 Line 09: If the WHILE loop exits due to reaching the end of array then it
returns false.

Binary searches only work on sorted sets of items!


Binary search

Version 2 1 © OCR 2020


How it could look in an exam Explanation
01 function BinarySearch(searchList, searchItem) In this example a function is created that takes two parameters
//Define the initial start and end points in the array  searchList: An array of items to be searched through
02 start = 0  searchItem: The item to be searched for
03 end = len(searchList)
How does it work?
The algorithm works by continually splitting the list in half until it finds the value it
//Keep looping until only one element left. is looking for or has eliminated all the options.
04 while start != end  Line 01: The function is defined with two parameters.
//Find the mid-point between start and end  Line 02/03: An initial value for start and end are set.
05 midPoint = (start + end) DIV 2  On the first pass, the start index is 0 and the end index is the final index
//If mid-point is greater, then value is to the left in the array.
06 if searchList[midPoint] > searchItem then  Line 05 finds the middle of the array.
07 start = midPoint + 1  Line 06 checks to see if the element in the array at the midpoint is greater
than the item we are looking for.
//If mid-point is less, then value is to the right a. If so, then Line 07 sets the index, one to the right of this position, as
08 elseif searchList[midPoint] < searchItem then
the new start.
09 end = midPoint – 1  Line 08 checks to see if the element in the array at the midpoint is less than
//If we find a match, return true the item we are looking for.
10 elseif searchList[midPoint] == searchItem then a. If so, then Line 09 sets the index, one to the left of this position, as
the new end.
11 return true
 Line 10 checks if item in the array and the item we are looking for are the
12 endif
same
13 endwhile  If so, then Line 11 returns true and the function exits.
//If no match is found then return false  If either Line 06 or Line 08 are true, the while LOOP repeats with either the
14 return false new start or end values.
15 endfunction  Line 14: By default, if the value is not found, false is returned.

Version 2 2 © OCR 2020


Bubble sort is an inefficient way of sorting an array!
Bubble sort
How it could look in an exam Explanation
01 function BubbleSort(sortList) In this example a function is created that takes one parameter
 sortList: An array of items to be sorted
//Set a flag to false to show array is not sorted
02 sorted = false How does it work?
03 while sorted == false The algorithm works by using a FOR loop, nested within a WHILE loop.
//Set the flag back to true (assume pass will sort array)  The WHILE loop keeps running until the array is sorted
04 sorted = true  The FOR loop is used to sort the elements within the array
//Step through the array, index by index
 Line 01: The function is defined with one parameter.
05 for sortCount = 0 to len(sortList) – 2
 Line 02: We create a ‘flag’ and set it to false – assuming the array is
//Check if the element is larger than the one next door
not sorted at the start of the algorithm.
06 if sortList[sortCount] > sortList[sortCount + 1] then  Line 03: We start the WHILE loop, which keeps running whilst the array
//If so – swap elements around using a ‘3-way’ swap is not sorted (sorted == false).
07 temp = sortList [sortCount]  Line 04: We assume that this next iteration will sort the array.
08 sortList[sortCount] = sortList[sortCount + 1]  Line 05: We use a FOR loop to iterate through the array.
09 sortList[sortCount + 1] = temp  Lines 06-09: If it finds an element is greater than the one next door to
the right, then it swaps these elements around.
//The array was not sorted, so set the flag back to false
 Line 10: Because a swap was made, the array is ‘unsorted’ and so the
10 sorted = false flag is set back to false.
11 endif  Line 12: The index is increased by 1, i.e. we move to the next index and
//Move along the array index and check next element Lines 06-09 are repeated.
12 next sortCount  Line 13: Once the FOR loop exits, the WHILE loop iterates.
13 endwhile  Line 03: The WHILE loop checks to see if sorted is false.
 If the sorted is true, we know the array is sorted, otherwise the
//Once sorted, return the array back
algorithm iterates again.
14 return sortList
 Line 14: Once sorted == true, we return the sorted list.
15 endfunction

Version 2 3 © OCR 2020


Insertion sort Insertion sort works well on mostly ordered arrays, but overall is an inefficient way of sorting an array!

How it could look like in an exam Explanation


01 function InsertionSort(sortList) In this example a function is created that takes one parameter
 sortList: An array of items to be sorted
//Start with the 2nd element in the list and carry on until the
last //element in the list How does it work?
02 for sortCount = 1 to len(sortList) - 1 The algorithm works by using a FOR loop with a WHILE loop
03 listIndex = sortCount nested inside.
 The FOR loop is used to move element by element
//While not at the start of the list AND the value in the index through the sortList from start to end.
position //is less than the one to left of it  The WHILE loop checks to see if an element in
04 while listIndex > 0 and sortList[listIndex] < sortList[listIndex-1] sortList is greater than the one to the left of it. It then
repeats this working backwards through the list, until the
//Swap the elements around element is in the correct place.
05 temp = sortList [listIndex]
06 sortList[listIndex] = sortList[listIndex + 1]
 Line 02/03: The FOR loop is created to run through the
07 sortList[listIndex + 1] = temp
array. Each time the sortCount is assigned to the
//Move back to the previous element (to the left) and test again listIndex variable. This means that later we can
08 listIndex = listIndex – 1 decrement the listIndex variable without affecting
09 endwhile the FOR loop pointer.
 Line 04: The WHILE loop checks to see if the value in
//Once the element is in the correct place, move to the next list index the current index is larger than the one to the left
10 next sortCount  Line 05/06/07: If so, we swap the values
 Line 08: We now need to check that the moved value is
//Return the sorted list
11 return sortList
in the correct place, so we reverse down the array,
checking and swapping if needed.
12 endfunction  Line 10: Once one element is sorted, we increase the
FOR loop counter to check the next element.
 Line 11: Finally, we return the sorted array.

Version 2 4 © OCR 2020


Merge sort is generally a fairly efficient way of sorting an array!
Merge sort
How it could look in an exam Explanation
Candidates do not need to recognise Exam Reference Language for a Merge
Sort. This is outside of the specification. How does it work?

They should be able to explain and carry out a merge sort in an exam. Splitting the array
 An algorithm will take an array
Step 1: Split the arrays into
3 2 7 1 8 5 6 4  It then finds the midpoint and splits the array into 2 arrays
sub-arrays of 1 element.
 If the mid-point is not a whole number (e.g. mid-point of 9 is 4.5)
then you can either use 4 or 5 as the mid-point.
3 2 7 1 8 6  This process is repeated until all we have left are sub-arrays of 1
5 4 item.

Rebuilding the sorted array

3 2 7 1 8 5 6 4  The algorithm then merges the 1 element sub-arrays into larger,


sorted arrays.
Step 2: Take each sub-  It does this by looking at the start of a pair of sub-arrays
3 2 7 1 8 5 6 array and merge into a new,  It places the lowest value into a new array. It repeats this looking at
4
sorted array. the next lowest in each list until all the items are merged.
 It continues merging the sub arrays until you are left with a single
Step 3: Repeat this process sorted list.
2 3 1 7 5 8 4 until a final, sorted array is
6
produced.

Tip: Not all arrays will have an even number of elements. You will need to
1 2 3 7 4 5 6
8 practise using a Merge Sort on both even and uneven numbers of elements
in an array.

Output: A sorted array


1 2 3 4 5 6 7
8

Version 2 5 © OCR 2020


Exam Reference Language algorithms (no commenting)

Linear Search Binary Search


01 function LinearSearch(searchList, searchItem) 01 function BinarySearch(searchList, searchItem)
02 searchIndex = 0 02 start = 0
03 while searchIndex < len(searchList)
03 end = len(searchList)
04 if searchList[searchIndex] == searchItem then
05 return true 04 while start != end
06 endif 05 midPoint = (start + end) DIV 2
07 searchIndex = searchIndex + 1
06 if searchList[midPoint] > searchItem then
08 endwhile
09 return false 07 start = midPoint + 1
10 endfunction 08 elseif searchList[midPoint] < searchItem then
09 end = midPoint – 1
10 elseif searchList[midPoint] == searchItem then
11 return true
12 endif
13 endwhile
14 return false
15 endfunction

Version 2 6 © OCR 2020


Bubble Sort ERL Insertion Sort ERL
01 function BubbleSort(sortList) 01 function InsertionSort(sortList)
02 sorted = false 02 for sortCount = 1 to len(sortList) - 1
03 while sorted == false 03 listIndex = sortCount
04 sorted = true 04 while listIndex > 0 and sortList[listIndex] <
05 for sortCount = 0 to len(sortList) – 2 sortList[listIndex-1]
06 if sortList[sortCount] > sortList[sortCount + 1] then 05 temp = sortList [listIndex]
07 temp = sortList [sortCount] 06 sortList[listIndex] = sortList[listIndex + 1]
08 sortList[sortCount] = sortList[sortCount + 1] 07 sortList[listIndex + 1] = temp
09 sortList[sortCount + 1] = temp 08 listIndex = listIndex – 1
10 sorted = false 09 endwhile
11 endif 10 next sortCount
12 next sortCount 11 return sortList
13 endwhile 12 endfunction
14 return sortList
15 endfunction

Note: OCR Exam Reference Language loops are inclusive when creating the boundary limits, i.e. for x = 0 to 9 will loop 10 times.

Version 2 7 © OCR 2020


Whether you already offer OCR qualifications, are new to OCR, or are considering switching from your current provider/awarding organisation, you can request more information by completing the Expression of Interest
form which can be found here: www.ocr.org.uk/expression-of-interest

Looking for a resource? There is now a quick and easy search tool to help find free resources for your qualification: www.ocr.org.uk/i-want-to/find-resources/

OCR Resources: the small print


OCR’s resources are provided to support the delivery of OCR qualifications, but in no way constitute an endorsed teaching method that is required by the Board, and the decision to use them lies with
the individual teacher. Whilst every effort is made to ensure the accuracy of the content, OCR cannot be held responsible for any errors or omissions within these resources.

Our documents are updated over time. Whilst every effort is made to check all documents, there may be contradictions between published support and the specification, therefore please use the
information on the latest specification at all times. Where changes are made to specifications these will be indicated within the document, there will be a new version number indicated, and a summary of
the changes. If you do notice a discrepancy between the specification and a resource, please contact us at: resources.feedback@ocr.org.uk.

© OCR 2020 - This resource may be freely copied and distributed, as long as the OCR logo and this message remain intact and OCR is acknowledged as the originator of this work. OCR acknowledges
the use of the following content: n/a
Please get in touch if you want to discuss the accessibility of resources we offer to support delivery of our qualifications: resources.feedback@ocr.org.uk

Version 2 8 © OCR 2020

You might also like