If your lookup array is sorted ascending / alphabetically, should you ALWAYS be using a Binary (vs Linear) search?

d0rian

Active Member
Joined
May 30, 2015
Messages
313
Office Version
  1. 365
I'm sitting here dumbfounded at the idea that I might have been doing things the dumb/slow way for 10 years. I've always used the MATCH function with 0 (exact match) as the 3rd (match_type) variable, e.g.
MATCH(lookup_value, lookup_array, 0).

Not sure why, that's just how I was taught. But I've recently learned about Binary vs Linear search, specifically how much faster the former is for large data sets, but with the one important stipulation being that the lookup_array must be sorted in ascending (or A-Z) order or the lookup may not return the correct value.

The speed difference is striking (my lookup arrays are often 20,000 to 100,000 rows long). So much so that it has me wondering whether I should be using a Binary search 100% of the time, even when it means adding the extra step of sorting the lookup_array ascending/alphabetical. I found this page that seems to suggest/test that exact theory (and seems to advocate doing exactly that.)

- Is that more or less correct?
- Are there any pitfalls to doing it this way?
- My lookup array values sometimes don't start in Row 1...e.g. there might be 4 empty (or null > "") cells before my lookup array values begin in A5 for example. Could that compromise this process? Are empty/null-value cells counted as "high" or "low" values for the purposes of ascending/alphabetical sort order?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If you only run one LOOKUP then the initial sort plus the LOOKUP will be costlier.
If you run more LOOKUPs, then at some point it will be better to sort first.
Which means: you will know because you know the number of records and the number of iterations.
 
Upvote 0
Without seeing your sheet layout, I would suggest trying something on the lines of
Excel Formula:
=INDEX(result range,MATCH(criteria, match range, --(INDEX(match range,MATCH(criteria, match range))= criteria)))
which will compare a binary match result to the criteria to see if it is an exact match, if it is then it will return the result based on that, if it is not an exact match then it will use an linear match instead.

Using this method means that the data doesn't need to be sorted, although depending on the order of the data it may not be any faster than what you are using.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top