1 2 3 |
|
What is the number of comparisons made in the execution of the
while
loop shown above?
Details and Assumptions
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Essentially, we keep doubling i unless it doesn't exceed n . The values compared in the while loop are
1 , 2 , 2 2 , . . . , 2 k , 2 k + 1 ,
where k is such that 2 k ≤ n < 2 k + 1 , or l o g 2 n − 1 < k ≤ l o g 2 n , so that k = ⌊ l o g 2 n ⌋ . In all , k + 2 = ⌊ l o g 2 n ⌋ + 2 comparisons were made.