Compute how many steps binary search would take to find an item in arrays of various sizes.
You mean that there is a maximum of 8 steps if the element exists in the array,
but 9 steps if the element does not exist?
Hello Melody!
YES!
We search number 1:set {1,…,193}(1)1>⌊1+1932⌋=97nonew set {1,…,97}(2)1>⌊1+972⌋=49nonew set {1,…,49}(3)1>⌊1+492⌋=25nonew set {1,…,25}(4)1>⌊1+252⌋=13nonew set {1,…,13}(5)1>⌊1+132⌋=7nonew set {1,…,7}(6)1>⌊1+72⌋=4nonew set {1,…,4}(7)1>⌊1+42⌋=2nonew set {1,2}(8)1>⌊1+22⌋=1nowe have found number 1 in the last setWe search number 1.5:set {1,…,193}(1)1.5>⌊1+1932⌋=97nonew set {1,…,97}(2)1.5>⌊1+972⌋=49nonew set {1,…,49}(3)1.5>⌊1+492⌋=25nonew set {1,…,25}(4)1.5>⌊1+252⌋=13nonew set {1,…,13}(5)1.5>⌊1+132⌋=7nonew set {1,…,7}(6)1.5>⌊1+72⌋=4nonew set {1,…,4}(7)1.5>⌊1+42⌋=2nonew set {1,2}(8)1.5>⌊1+22⌋=1yesnew set {2}(9)1.5=2nowe have not found number 1.5 in the last set
Compute how many steps binary search would take to find an item in arrays of various sizes.
Let n = size of the array
Let c = comparisons in the worse case
c=1+log2(n)|log2(n)=ln(n)ln(2)c=1+ln(n)ln(2)n=193c=1+ln(193)ln(2)c=1+5.262690188900.69314718056c=1+7.59245703727c=8.59245703727c≈9
No more than 9.
Thanks Heureka but can you explain to me why you have to add the 1 ?
Hi EMP,
I did one of these for you the other day :)
Just assume that it is the very last one that is the one you are looking for and see how lon it would take.
193 in the array.
Let the first on be in postion 0 and the last one be in position 193
1. (0+193)/2 = 96 rounded down
2. (0+96)/2 = 48
3. (0+48)/2 = 24
4. (0+24)/2 = 12
5. (0+12)/2= 6
6. 6/2=3
7. 3/2=1
8. 1/2=0
That is 8
So I think that 9 will do it.
Thanks Heureka but can you explain to me why you have to add the 1 ?
Hello Melody!
The last compare on equality, if not equal, the name does not exist in the array.
Thanks Heureka,
You mean that there is a maximum of 8 steps if the element exists in the array, but 9 steps if the element does not exist?
You mean that there is a maximum of 8 steps if the element exists in the array,
but 9 steps if the element does not exist?
Hello Melody!
YES!
We search number 1:set {1,…,193}(1)1>⌊1+1932⌋=97nonew set {1,…,97}(2)1>⌊1+972⌋=49nonew set {1,…,49}(3)1>⌊1+492⌋=25nonew set {1,…,25}(4)1>⌊1+252⌋=13nonew set {1,…,13}(5)1>⌊1+132⌋=7nonew set {1,…,7}(6)1>⌊1+72⌋=4nonew set {1,…,4}(7)1>⌊1+42⌋=2nonew set {1,2}(8)1>⌊1+22⌋=1nowe have found number 1 in the last setWe search number 1.5:set {1,…,193}(1)1.5>⌊1+1932⌋=97nonew set {1,…,97}(2)1.5>⌊1+972⌋=49nonew set {1,…,49}(3)1.5>⌊1+492⌋=25nonew set {1,…,25}(4)1.5>⌊1+252⌋=13nonew set {1,…,13}(5)1.5>⌊1+132⌋=7nonew set {1,…,7}(6)1.5>⌊1+72⌋=4nonew set {1,…,4}(7)1.5>⌊1+42⌋=2nonew set {1,2}(8)1.5>⌊1+22⌋=1yesnew set {2}(9)1.5=2nowe have not found number 1.5 in the last set