How do searching and sorting algorithms work, and how do we judge which is more efficient?
Describe and implement the standard searching and sorting algorithms (linear and binary search, bubble, insertion and quicksort), explain recursion, and compare algorithm efficiency.
A focused answer to the SQA Advanced Higher Computing Science content on standard algorithms, covering linear and binary search, bubble, insertion and quicksort, recursion, and comparing the efficiency of algorithms.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
The SQA wants you to know the standard searching and sorting algorithms, to be able to trace and implement them, to explain recursion, and to compare how efficient algorithms are as the data grows. Efficiency, not just correctness, is examined at Advanced Higher.
Searching algorithms
A linear (sequential) search examines each item from the start until it finds the target or reaches the end. It works on any list, sorted or not, but is slow on large lists because it may check every item.
A binary search is much faster but needs a sorted list. It compares the target with the middle item; if they match it stops, otherwise it discards the half that cannot contain the target and repeats on the remaining half. Each step halves the search space.
Sorting algorithms
A bubble sort makes repeated passes, comparing each adjacent pair and swapping them if they are out of order; the largest unsorted value "bubbles" to the end each pass. It is simple but slow, doing on the order of n-squared comparisons.
An insertion sort keeps a growing sorted section at the front and takes each next item, shifting larger items right until it can drop the new item into its correct place. It is efficient on nearly-sorted data but still on the order of n-squared in the worst case.
A quicksort chooses a pivot, partitions the remaining items into those smaller and those larger than the pivot, then sorts each partition the same way (recursively). It averages about n log n, much faster than bubble or insertion sort on large lists.
Recursion
Recursion is when a routine calls itself to solve a smaller instance of the same problem. Every recursive routine needs two parts: a base case that returns directly without recursing (so the process stops), and a recursive case that reduces the problem and calls itself. Recursion expresses naturally divide-and-conquer algorithms such as quicksort and structures such as traversing a tree.
Comparing efficiency
Efficiency describes how an algorithm's work grows as the input size n grows, independent of the particular machine. A linear search grows in proportion to n; a binary search grows in proportion to log n; bubble and insertion sort grow in proportion to n-squared; quicksort averages n log n. The faster-growing the cost, the worse the algorithm scales, so for large n the difference between n-squared and n log n is enormous. You compare algorithms by this growth, not by timing a single run on one machine.
Try this
Q1. State the precondition a binary search requires. [1 mark]
- Cue. The list must be sorted.
Q2. State roughly how the number of comparisons grows with list size n for a linear search. [1 mark]
- Cue. In proportion to n (up to n comparisons), because it may check every item.
Exam-style practice questions
Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AH style: binary search4 marksExplain why a binary search is faster than a linear search on a large list, and state the one condition a binary search requires.Show worked answer →
A linear search checks items one at a time from the start, so on average it inspects about half the list and at worst all of it (1 mark). A binary search checks the middle item and discards half the remaining list at each step, so it inspects only about log-base-2 of n items (1 mark); for a list of a million items that is about 20 comparisons versus up to a million (1 mark). The condition is that the list must already be sorted, because binary search relies on knowing which half to discard (1 mark).
AH style: recursion3 marksDefine recursion and state the one feature every recursive algorithm must have to avoid running forever.Show worked answer →
Recursion is when a procedure or function calls itself to solve a smaller version of the same problem (1 mark). Every recursive algorithm must have a base case (stopping condition) that is reached without a further recursive call (1 mark), so the recursion terminates instead of calling itself endlessly and overflowing the call stack (1 mark). Markers reward the self-call definition plus the base-case requirement.
Related dot points
- Describe and implement the data structures used in Advanced Higher: one- and two-dimensional arrays, records, sequential files, linked lists, stacks and queues, including their operations.
A focused answer to the SQA Advanced Higher Computing Science content on data structures, covering one- and two-dimensional arrays, records, sequential files, linked lists, stacks and queues with their core operations.
- Describe and implement object-oriented programming using classes, objects, instantiation, attributes and methods, encapsulation, inheritance, and polymorphism through method overriding.
A focused answer to the SQA Advanced Higher Computing Science content on object-oriented programming, covering classes and objects, instantiation, encapsulation, inheritance and polymorphism through method overriding, with UML class diagrams.
- Describe and compare iterative (agile) and structured (waterfall) development methodologies, and apply the analysis, design, implementation, testing, documentation, evaluation and maintenance stages of the software development process.
A focused answer to the SQA Advanced Higher Computing Science content on development methodologies, covering iterative agile and structured waterfall approaches and every stage of the software development process from analysis to maintenance.
- Plan and carry out testing using normal, extreme and exceptional data and dry runs with trace tables, and evaluate software for fitness for purpose, robustness, efficiency and maintainability.
A focused answer to the SQA Advanced Higher Computing Science content on testing and evaluation, covering normal, extreme and exceptional test data, dry runs and trace tables, and evaluating software for fitness for purpose, robustness, efficiency and maintainability.