Sub topic 12
Binary search on sorted data
This chapter assumes you can write XLOOKUP with all six arguments and that you know what match_mode does. It covers the last argument, search_mode, and the two values that switch XLOOKUP to a binary search.
Linear search versus binary search
By default XLOOKUP uses search_mode 1. It starts at the first cell and checks each cell in order until it finds a match. On a list of 100,000 rows, a value near the bottom takes about 100,000 comparisons.
A binary search needs the list to be sorted. It compares the lookup value with the middle cell and discards the half that cannot contain the answer. It repeats this on the remaining half until it finds the value or runs out of cells.
The number of steps grows slowly with list size. A list of 1,000 rows needs at most about 10 comparisons. A list of 1,000,000 rows needs about 20.
The two binary search modes
| search_mode | Behavior | Data must be |
|---|---|---|
| 1 | Linear, first to last (default) | Any order |
| -1 | Linear, last to first | Any order |
| 2 | Binary search | Sorted ascending |
| -2 | Binary search | Sorted descending |
Writing the formula
Suppose A2:A1000 holds thresholds sorted from smallest to largest, and B2:B1000 holds the rate for each threshold.
=XLOOKUP(E2, A2:A1000, B2:B1000, "Not found", -1, 2)The arguments are, in order:
E2is the value to find.A2:A1000is the sorted lookup range.B2:B1000is the range to return from."Not found"is theif_not_foundtext.-1ismatch_mode, which means exact match or the next smaller item.2issearch_mode, which means binary search, ascending.
If you only need an exact match, set match_mode to 0 and keep search_mode at 2.
The main gotcha: unsorted data fails silently
Binary search does not check that your data is sorted. If the data is out of order, the search can discard the half that holds the answer. Two things can then happen:
- A value that exists returns
#N/Aor yourif_not_foundtext. - An approximate lookup returns the wrong row, with no error.
Because nothing signals the problem, a wrong answer looks like a good one. Sorting fixes it. Match the sort direction to the mode: ascending for 2, descending for -2.
Duplicates are another trap. If the sorted list holds the same value several times, a binary search does not promise which of those rows you get. If you need the first or last duplicate, use a linear search with search_mode 1 or -1.
When to use it
Binary search pays off only when both of these are true:
- The list is large, in the tens of thousands of rows or more.
- The workbook runs many lookups against that list, for example one XLOOKUP per row in a big table.
On a few hundred rows, linear and binary search feel the same, and the risk of unsorted data outweighs any gain. The five-row grade table from the previous chapters is a poor fit for binary search. Keep search_mode at its default there.
Sorted data is common in some places. Lookups on dates, ascending IDs and numeric thresholds often fit. If new rows are added at the bottom in arbitrary order, they do not.
Summary of decisions
- Use
search_mode1 or -1 when the data is unsorted, small, or has duplicates where position matters. - Use 2 for large ascending lists and -2 for large descending lists.
- Pair either binary mode with
match_mode0, -1 or 1 as your lookup requires.
Exercise
A2:A50000 holds employee IDs sorted from smallest to largest, and C2:C50000 holds their departments. Write a formula that returns the department for the ID in F2 using a binary search, with "Unknown ID" shown when the ID is missing. What happens if someone later sorts the IDs in descending order and the formula stays unchanged?
Show answer
=XLOOKUP(F2, A2:A50000, C2:C50000, "Unknown ID", 0, 2). After the descending sort, the binary search can discard the wrong half, so existing IDs may return "Unknown ID" with no error. Change search_mode to -2, or re-sort the IDs ascending.