Binary Search: Halving an Ordered List | Edexcel IAL Maths notes
Free preview: Binary Search: Halving an Ordered List
Halving an Ordered List
Unit D1 (WDM11): Decision Mathematics 1
Let's start with something you have done many times. To find a word in a dictionary, you don't read from page 1. You open it near the middle, see whether your word comes before or after, and throw away the half it can't be in.
- Binary search is that idea written as an algorithm. It finds a target item in an ordered list, or shows that it is not there.
- "Binary" means two: every pass splits what is left into two halves and keeps only one of them.
- The list must be in order first (alphabetical or numerical). If it isn't, sort it first, for example with a quick sort.
- Why does order matter? Because "the target comes before the middle item" only tells you it is in the left half if the list is sorted.
Why it is so fast
- Reading a list from the start (a linear search) checks items one at a time. A list of 1000 items could need 1000 checks.
- Binary search throws away about half of what is left after every check. 1000 items become 500, then 250, then 125, ... Very soon there is nothing left to search.
- You will see on page 4 that 1000 items never need more than 10 passes.
The middle item rule
- We keep track of the part of the list still being searched by its first and last positions, f and l.
- The middle position is . If this is not a whole number, round up.
- Example: f = 1 and l = 10 gives , so m = 6. f = 7 and l = 10 gives , so m = 9.
๐ก In plain words
- With an even number of items left, take the one just right of the middle.
- This is the same rule as the quick sort pivot.
๐ Watch me do it: find the middle position for three different parts of a list
| What I write | Why I do it |
|---|---|
| f = 1, l = 13: , so m = 7. | 13 items (an odd number), so there is an exact middle: 6 items each side. |
| f = 8, l = 13: , so m = 11. | Not whole, so round up. |
| f = 5, l = 5: , so m = 5. | Only one item left, and it is the middle. |
๐ Here's the trap
- Right: the middle position is , using positions in the original list.
- Wrong: averaging the values in the list, or counting positions from 1 again after throwing half away. Positions never get renumbered.
โ๏ธ YOUR TURN 1
Try this first:
Find the middle position m for each part of a list.
- f = 1, l = 20
- f = 11, l = 20
- f = 11, l = 14
- f = 3, l = 4
Answer:
- , so m = 11.
- , so m = 16.
- , so m = 13.
- , so m = 4.
The Algorithm and Recording Each Pass
The algorithm
- Let f = 1 and l = n, where n is the number of items in the list. Why: at the start, the whole list is in play.
- If f > l, stop: the target is not in the list. Why: the part left to search is empty.
- Let m be the middle position, rounded up if necessary.
- If the item in position m is the target, stop: the target is found.
- If the target comes before the item in position m, let l = m โ 1. Otherwise let f = m + 1.
- Go back to step 2.
- Notice the โ 1 and + 1 in step 5. The middle item has just been checked, so it is thrown away too.
- Each pass checks one item and removes at least half of what is left.
Recording each pass
Exam questions say "show each pass" or "make your method clear". The safest way is a table with one row per pass. Let's search this ordered list of 11 names for Beth.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Name | Amir | Beth | Carl | Dina | Ella | Finn | Gita | Hugo | Iris | Jack | Kara |
๐ Watch me do it: search the 11 names for Beth
| What I write | Why I do it |
|---|---|
| Pass 1: f = 1, l = 11, , Finn. Beth is before Finn: l = 5. | Throws away Finn and everything after it: 6 names gone in one comparison. |
| Pass 2: f = 1, l = 5, , Carl. Beth is before Carl: l = 2. | Keep positions 1 to 2. |
| Pass 3: f = 1, l = 2, , so 2, Beth. Found. | This is where the rounding matters. 1.5 rounds up to 2, so we check Beth straight away. |
Unlock the full note: start 7 days free.
Log in or subscribe to read the rest of this note. Members also get the AI tutor, practice quizzes, mock exams and flashcards for this topic.
Log in ยท Subscribe ยท Start free trial
All notes in D1.1 Algorithms, Sorting, Searching and Packing
- What Is an Algorithm? Flow Charts, Pseudocode and Trace Tables
- Bubble Sort and Quick Sort Step by Step
- Binary Search: Halving an Ordered List
- Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
What's inside for this topic
4 teacher notes, 103 practice questions, 38 flashcards, 2 quizzes.
Get the full notes, every practice question, flashcards, quizzes and mock papers.
Previous note: Bubble Sort and Quick Sort Step by Step Next note: Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins