KS48525 3.1.2, 3.1.3 30 min read 10 question quiz 16 exam marks

Efficiency and Searching Algorithms

What efficiency actually means for an algorithm, how linear and binary search work step by step, and the exact conditions under which each one is the right choice.

What efficiency means

Two algorithms can produce exactly the same correct answer and still be worlds apart in usefulness. The efficiency of an algorithm is how much of a resource it consumes to get that answer, and at GCSE the resource that matters is the number of steps, which translates into time.

The reason this is worth measuring is that the difference between algorithms grows with the size of the data. On ten items nothing matters. On ten million items the choice of algorithm decides whether a program is instant or unusable.

How to compare efficiency without maths

Count the number of comparisons each algorithm makes on the same data, in the worst case. The one that makes fewer comparisons as the list grows is the more efficient algorithm.

Worst case, best case

  • Best case is the luckiest possible input. For linear search that is the item being first.
  • Worst case is the least lucky input. For linear search that is the item being last, or absent altogether.

Examiners nearly always ask about the worst case, because the best case is a matter of luck and tells you nothing useful about the algorithm.

The point that earns the mark

A faster algorithm does not need faster hardware. Changing from linear search to binary search on a large sorted list makes a program dramatically quicker on exactly the same machine, because it does less work rather than doing the same work faster. That is the sentence to write when a question asks why algorithm choice matters.

A linear search starts at the beginning of the list and compares each item in turn with the value being looked for, stopping when it finds a match or when it reaches the end of the list.

AQA PSEUDO-CODE
SUBROUTINE linearSearch(items, target)
    FOR i ← 0 TO LEN(items) - 1
        IF items[i] = target THEN
            RETURN i
        ENDIF
    ENDFOR
    RETURN -1
ENDSUBROUTINE

Searching for 23 in [4, 8, 15, 16, 23, 42] compares 4, then 8, then 15, then 16, then 23. Five comparisons, then it stops.

The properties that matter

  • The list does not have to be sorted. This is linear search's one real advantage and it is a big one.
  • The worst case is n comparisons on a list of n items, when the target is last or missing.
  • It is simple to write and hard to get wrong.
When linear search is the right choice

When the data is unsorted, when the list is short, or when the list changes so often that keeping it sorted would cost more than the searches save.

Linear search and binary search, looking for 38Two ways of finding the number 38 in a sorted list of fifteen numbers. In a linear search the program looks at the items one at a time from the left, so it has to look at the first nine and reject each one before it finds 38 on the tenth look. In a binary search the program checks the middle item, which is 30. 38 is bigger, so the whole left half is thrown away. It then checks the middle of what is left, which is 47. 38 is smaller, so the right half is thrown away, leaving three items. The middle of those is 38, found on the third look. Binary search takes 3 looks where linear search took 10, and the gap grows as the list grows, but binary search only works if the list is sorted.Linear search: look at every item in turn381114172126303338424751566012345678910not it, not it, not it ...found it on the 10th look1Binary search: halve the sorted list every time3811141721263033384247515660check the middle: 30. 38 is bigger, so drop the left half3811141721263033384247515660check the middle: 47. 38 is smaller, so drop the right half3811141721263033384247515660check the middle: 38. Found it, on the 3rd look2linear: 10 looksbinary: 3 looksbinary search needs a sorted list3
  1. Linear search looks at every item in turn from the start, so it can be slow.
  2. Binary search checks the middle and throws half the list away each time.
  3. 3 looks against 10. The longer the list, the bigger the gap, but binary search needs the list sorted.
Linear works on any list. Binary is far faster, but only on a sorted one.

A binary search works on a sorted list. It compares the target with the middle item, and then discards half the list.

  1. Find the middle item of the list.
  2. If it is the target, stop.
  3. If the target is smaller than the middle item, repeat using only the left half.
  4. If the target is larger, repeat using only the right half.
  5. If there are no items left to check, the target is not in the list.

Traced on a real list

Searching for 23 in the sorted list [4, 8, 15, 16, 23, 42]:

StepList being searchedMiddle itemComparisonAction
14, 8, 15, 16, 23, 421523 is greater than 15Keep the right half: 16, 23, 42
216, 23, 422323 equals 23Found, stop

Two comparisons instead of five. On six items that is barely worth mentioning. The reason binary search matters is what happens as the list grows.

Items in listLinear search, worst caseBinary search, worst case
1001007
1 0001 00010
1 000 0001 000 00020
1 000 000 0001 000 000 00030

Every time the list doubles, binary search needs just one extra comparison, because one extra comparison halves the list one more time. Linear search needs twice as many.

Binary search requires a sorted list

This is the condition examiners test. If a question describes data arriving in a random order and never being sorted, binary search is not available, however efficient it would be.

Choosing between them in a scenario question

Ask two questions of the scenario. Is the data sorted? How big is it? Sorted and large means binary. Unsorted, or so small that sorting would cost more than searching, means linear. Say which condition in the scenario made you choose, because that is where the mark is.

The hidden cost

If the data is not already sorted, binary search is only worth it when you will search many times. Sorting a list takes considerably more work than one linear search, so sorting purely to run a single binary search is slower overall than just doing the linear search.

Binary search halving the list three timesBinary search looking for 12 in the sorted list 2, 5, 8, 12, 16, 23, 38, 56, 72, 91, 100. Step one sets low to the first item and high to the last, giving a midpoint of 23. The target 12 is smaller than 23, so the whole right half including 23 is discarded. Step two searches 2, 5, 8, 12, 16 with a midpoint of 8. The target 12 is bigger than 8, so 8 and everything to its left is discarded. Step three searches 12 and 16 with a midpoint of 12, which is the target, so the search succeeds after three comparisons rather than the four a linear search would need.Binary search for 12 in a sorted list of 11 items2low58121623mid38567291100highmidpoint is 23. 12 is smaller, so throw away 23 and everything right2low58mid1216high2338567291100midpoint is 8. 12 is bigger, so throw away 8 and everything left25812lowmid16high2338567291100midpoint is 12. Found it, after only three comparisons
Binary search only works on a sorted list. Doubling the list length adds just one extra comparison, which is why it scales so well.

Key terms you must be able to define

Efficiency
A measure of how much of a resource, usually time or number of steps, an algorithm uses to solve a problem.
Linear search
A search that examines each item in a list in turn until the target is found or the end of the list is reached.
Binary search
A search on a sorted list that repeatedly compares the target with the middle item and discards the half that cannot contain it.
Worst case
The input that causes an algorithm to perform the greatest number of steps.
Comparison
One test of whether an item matches or is greater or less than the value being searched for, used as the unit for measuring search efficiency.
Sorted list
A list whose items are in ascending or descending order, which is the condition binary search requires.
Middle item
The item at the centre of the section of the list currently being searched by a binary search.

Flashcards

Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.

DefineEfficiency
AnswerA measure of how much of a resource, usually time or number of steps, an algorithm uses to solve a problem.
DefineLinear search
AnswerA search that examines each item in a list in turn until the target is found or the end of the list is reached.
DefineBinary search
AnswerA search on a sorted list that repeatedly compares the target with the middle item and discards the half that cannot contain it.
DefineWorst case
AnswerThe input that causes an algorithm to perform the greatest number of steps.
DefineComparison
AnswerOne test of whether an item matches or is greater or less than the value being searched for, used as the unit for measuring search efficiency.
DefineSorted list
AnswerA list whose items are in ascending or descending order, which is the condition binary search requires.
DefineMiddle item
AnswerThe item at the centre of the section of the list currently being searched by a binary search.

Reach the top grade on this topic

The knowledge on this topic is small and the marks are almost all in the comparison.

Never say binary search is 'better'. Say it is more efficient on a large sorted list, and then name the condition it needs. An answer that recommends binary search without checking that the data is sorted is throwing away the whole point of the question.

Quantify the difference. "Binary search is faster" is one mark at most. "Binary search halves the number of remaining items with each comparison, so a list of one million needs about twenty comparisons rather than up to one million" is the top level answer.

Handle the sorting cost honestly. Top candidates notice that if the data must be sorted first, and it is only searched once, linear search wins. That single observation is often the difference between four marks and six on an evaluate question.

  • Trace a binary search on a given sorted list and count the comparisons correctly
  • State the one advantage of linear search in a single sentence
  • Explain why binary search only needs one more comparison when the list doubles in size
  • Decide between the two algorithms for a described scenario and justify it using the scenario's own details

Mistakes that cost marks here

Avoid these
  • Recommending binary search on unsorted data. Binary search is only defined for a sorted list.
  • Saying binary search 'splits the list in half once'. It halves the remaining list repeatedly, at every comparison.
  • Claiming linear search has no advantages. It works on unsorted data and it is faster than sort plus binary search for a one off lookup.
  • Comparing algorithms on tiny lists and concluding one is better. Efficiency questions are about behaviour as the list grows.
  • Counting the comparisons wrongly in a trace by forgetting that the middle item is itself compared before the list is halved.
Fun fact

Binary search on a list of one billion items needs at most 30 comparisons. Linear search on the same list needs up to one billion. That is the difference between a task finishing before you have looked up from the keyboard and a task you would abandon.

Knowledge check

0 of 10 answered
1What condition must be met before a binary search can be used?
2A linear search is performed on a list of 500 unsorted names. What is the maximum number of comparisons needed?
3Roughly how many comparisons does a binary search need in the worst case on a sorted list of 1000 items?
4Which is the single advantage of linear search over binary search?
5A binary search is looking for 30 in [10, 20, 30, 40, 50, 60, 70]. Which item is compared first?
6If a sorted list doubles in size, how does the worst case for a binary search change?
7Which statement about the efficiency of an algorithm is correct?
8Data arrives in a random order and each item is searched for only once before the data is replaced. Which approach is best?
9How many comparisons does a linear search need to find 4 at the start of [4, 8, 15, 16, 23, 42]?
10Why is the worst case, rather than the best case, normally used to compare algorithms?
Score 0 / 10

Exam-style questions

Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.

16 marks
1Describe how a binary search finds a value in a sorted list.4 marks
Describe

Mark scheme0 / 4
  • LThe middle item of the list is examined
  • LIf it matches the target the search stops
  • LIf the target is smaller the upper half is discarded, and if larger the lower half is discarded
  • LThe process repeats on the remaining half until the item is found or no items remain
Model answer

A binary search first examines the item in the middle of the sorted list. If that item is the one being searched for, the search stops and reports success. If the target is smaller than the middle item then every item to the right of the middle can be discarded, and if it is larger then every item to the left can be discarded, because the list is in order. The search then repeats on whichever half remains, examining its middle item, and continues halving until the target is found or until no items are left, at which point the value is known not to be in the list.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2A list of 1024 sorted values is searched. State the maximum number of comparisons required by a linear search and by a binary search.2 marks
State

Mark scheme0 / 2
  • LLinear search requires up to 1024 comparisons
  • LBinary search requires up to 10 or 11 comparisons
Model answer

A linear search must compare every item in the worst case, so it requires up to 1024 comparisons. A binary search halves the remaining list at each step, and 1024 can be halved ten times to reach a single item, so it requires at most about ten or eleven comparisons.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3Explain one advantage of a linear search over a binary search.2 marks
Explain

Mark scheme0 / 2
  • LA linear search does not require the data to be sorted
  • LSo it can be used on data that arrives in a random order, or where sorting would cost more time than it saves
Model answer

The advantage of a linear search is that it does not require the list to be in any particular order, whereas a binary search only works on sorted data. This means a linear search can be used immediately on data that arrives in a random order, and for a list that is searched only once it is often faster overall, because sorting the list first would take considerably more time than the linear search itself.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4A company stores 5 million customer records, sorted by customer number, and looks up records thousands of times each hour. Justify which search algorithm they should use.5 marks
Justify

Mark scheme0 / 5
  • LBinary search should be used
  • LThe data is already sorted, which is the condition binary search requires
  • LBinary search halves the number of remaining records with each comparison
  • LSo it needs about 23 comparisons rather than up to 5 million
  • LThe saving is repeated on every one of thousands of lookups per hour
Model answer

The company should use a binary search. The records are already sorted by customer number, so the one condition that binary search requires is satisfied and no sorting cost has to be paid. Binary search compares the target with the middle record and then discards half of the remaining records, repeating until the record is found, so the number of comparisons grows extremely slowly as the data grows. On five million records it needs roughly twenty three comparisons in the worst case, whereas a linear search would need up to five million. Because the system performs thousands of lookups every hour, that saving is multiplied thousands of times over, so the difference between the two algorithms is the difference between an instant response and a system that cannot keep up.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
5Explain why comparing the efficiency of two algorithms is more useful than comparing how long each takes to run on a particular computer.3 marks
Explain

Mark scheme0 / 3
  • LRun time depends on the hardware the program is tested on
  • LEfficiency counts the number of steps or comparisons, which is independent of the machine
  • LSo efficiency predicts how the algorithm will behave as the amount of data grows
Model answer

The time a program takes to run depends on the computer it is tested on, so a poor algorithm on a fast processor can appear quicker than a good algorithm on a slow one, which tells you nothing about the algorithms themselves. Measuring efficiency instead counts the number of steps or comparisons the algorithm performs, and that count is a property of the algorithm rather than of the hardware. It also shows how the work grows as the data set grows, which is what actually matters, because two algorithms that look identical on a hundred items can differ by a factor of thousands on a million.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 16