Binary search: the math behind 32 steps

  • Thread starter Thread starter ducmod
  • Start date Start date
  • Tags Tags
    Binary Search
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
25K
  • · Replies 93 ·
4
Replies
93
Views
17K
  • · Replies 67 ·
3
Replies
67
Views
18K
  • · Replies 83 ·
3
Replies
83
Views
23K
  • · Replies 62 ·
3
Replies
62
Views
13K
  • · Replies 71 ·
3
Replies
71
Views
15K