Binary search: the math behind 32 steps

  • Thread starter Thread starter ducmod
  • Start date Start date
  • Tags Tags
    Binary Search
Click For Summary

Discussion Overview

The discussion revolves around understanding the mathematical basis of binary search, specifically how the number of steps required to find an entry in a large dataset relates to the logarithm of the number of entries. Participants explore the implications of this relationship using examples, including a hypothetical phone book with 4 billion entries and a side exercise involving a tournament structure.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Conceptual clarification

Main Points Raised

  • One participant queries how to compute the maximum number of steps required for binary search in a dataset of 4 billion entries, noting that it is stated to be 32 steps.
  • Another participant emphasizes the importance of powers of 2 in understanding binary search and suggests experimenting with smaller datasets.
  • A side exercise is introduced regarding the number of matches in a tournament with 512 contestants, with a participant calculating that it would take 9 steps (or rounds) in the worst-case scenario.
  • Further discussion leads to a participant asking how to calculate the total number of matches in the tournament, indicating a connection to the original topic.
  • Another participant asserts that the number of steps in binary search is equal to the base-2 logarithm of the number of entries, providing examples of logarithmic calculations.

Areas of Agreement / Disagreement

Participants generally agree on the logarithmic relationship in binary search, but there is no consensus on the specific calculations or implications for different datasets, as some aspects remain exploratory and uncertain.

Contextual Notes

Some participants express uncertainty about the calculations involved in binary search and the side exercise, indicating that further clarification may be needed. The discussion includes various assumptions about the dataset and the conditions under which binary search operates.

Who May Find This Useful

Readers interested in algorithms, particularly binary search, mathematical reasoning related to logarithms, and those seeking to understand the implications of dataset size on search efficiency may find this discussion beneficial.

ducmod
Messages
86
Reaction score
0

Homework Statement



Hello!
I have just heard in one online lecture that if for example there is a phone book with 4 billion entries (or a data set
with 4 billion entries), then to find one entry (for example, Kim Simth) I need maximum 32 steps.
If the amount of entries is increased by half, ie to 8 biliion, then it's ll take 33 steps.

I understand the algorithm of how to do binary search (divide by half, by half, etc). But I have no clue on how to
compute the math behind those 32 steps, and how log(n) works in this case.

Please, help me with your advice on how to learn this, what are good resources on this matter, where to read about
it (not in Wikipedia, sorry).

Thank you very much!
 
Physics news on Phys.org
Powers of 2 are very powerful ! Play around with 8, 16 entries to see how itt works. Note that the array has to be sorted in order to do this.

Side exercise: if there are 512 contestants at Wimbledon, how many matches have to be played until Djoko gets the first prize ? And how many rounds does that take ? Same thing !
 
BvU said:
Powers of 2 are very powerful ! Play around with 8, 16 entries to see how itt works. Note that the array has to be sorted in order to do this.

Side exercise: if there are 512 contestants at Wimbledon, how many matches have to be played until Djoko gets the first prize ? And how many rounds does that take ? Same thing !
I am not sure I correctly understood the nice Wimbledton side task :) Is it 9 steps in worst case scenario? 512 = 2^9, i.e. log 2( 512) = 9
 
Nine rounds is right. How would you calculate the total number of matches that it takes ? (this doesn't help with your original post, but it's a nice thingy to carry with you for later on).

Back to the BS (:smile:): If it takes nine steps for 29 entries, how many steps for 232 ? How much is 232 ? The math is really simple, actually! And the number of steps is the 2 logarithm of the number of entries. Piece of cake!
 
BvU said:
And the number of steps is the 2 logarithm of the number of entries.
In other words, the number of steps = ##\log_2(\text{entries})##

Here ##\log_2(4) = 2, \log_2(8) = 3##, and so on.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 86 ·
3
Replies
86
Views
24K
  • · Replies 93 ·
4
Replies
93
Views
16K
  • · Replies 67 ·
3
Replies
67
Views
16K
  • · Replies 83 ·
3
Replies
83
Views
22K
  • · Replies 62 ·
3
Replies
62
Views
12K
  • · Replies 71 ·
3
Replies
71
Views
14K