Best, Average, Worst complexities of a binary search?
Computer Science and ICT discussion, revision, exam and homework help.
-
Re: Best, Average, Worst complexities of a binary search?
What are you asking? Best case is O(1). You could get lucky and the element you want is in the middle of the list.
Worst case is O(log2(n)) as the number of times you can divide the list up in 2 is the maximum times you'll have to compare elements in a binary search.
Average case is also O(log2(n)). The average number of times you would compare elements in a binary search is halfway between 1 and log2(n), so it's 0.5*log2(n). But the "0.5*" is considered insignificant compared to the rest, so it ends up being O(log2(n)) as well. -
Re: Best, Average, Worst complexities of a binary search?Your conclusion is correct, but not how you got there. The average number of comparisons in a binary search is, in fact, closer to(Original post by tooosh)
The average number of times you would compare elements in a binary search is halfway between 1 and log2(n), so it's 0.5*log2(n). But the "0.5*" is considered insignificant compared to the rest, so it ends up being O(log2(n)) as well.
than
. You can't just assume that the average case is half of the worst case.
http://sce.uhcl.edu/yang/teaching/cs...y%20search.pdfLast edited by Planto; 04-06-2012 at 10:00. -
Re: Best, Average, Worst complexities of a binary search?I see. Never really had to consider the average case before.(Original post by Planto)
Your conclusion is correct, but not how you got there. The average number of comparisons in a binary search is, in fact, closer to
than
. You can't just assume that the average case is half of the worst case.
http://sce.uhcl.edu/yang/teaching/cs...y%20search.pdf