What Is the Correct Derivation of Binary Search Complexity?

In summary, the conversation discusses the recursive relation T(n) = 1 + T(n/2) and finding the pattern T(n) = 2i-1 + T(n/2i). It also mentions choosing i = log2 n and simplifying the first part of the equation to n-1. However, there is some confusion about where the pattern came from and how to get the simplified equation. The conversation also touches on the complexity of binary search, which is O(logn) if the list is ordered and the process of narrowing down the search by dividing the list into smaller parts.
  • #1
apiwowar
96
0
i found the recursive relation which is T(n) = 1 + t(n/2)

after a couple of substitutions i found the pattern which is T(n) = 2i-1 + T(n/ 2i)

i chose i = log2 n and then when i plugged it in i got

T(n) - 2log2(n -1) + T(1)

but doesn't the first part simplify to n-1?

the complexity of binary search is O(logn)

where did i mess up?
 
Physics news on Phys.org
  • #2
apiwowar said:
i found the recursive relation which is T(n) = 1 + t(n/2)
Should be T(n) = 1 + T(n/2), right? Also, in a recursive relationship, there has to be some number at which recursion stops. In that case, it would be T(1), I think, because at some point n/2 will be less than 1.
apiwowar said:
after a couple of substitutions i found the pattern which is T(n) = 2i-1 + T(n/ 2i)
Where did this come from? Just above, you have T(n) = 1 + T(n/2).
apiwowar said:
i chose i = log2 n and then when i plugged it in i got

T(n) - 2log2(n -1) + T(1)
How did you get this?
apiwowar said:
but doesn't the first part simplify to n-1?

the complexity of binary search is O(logn)

That's right, assuming the list is in order. Think about a list of 32 numbers. By dividing the list in two, you have two sublists of 16 numbers each. It's pretty easy to tell whether the number you're looking for is in the first list or the second, so you can narrow the search down to on of the 16-element lists.

Now, divide that list into two 8-element lists. Determine which of these lists the number needs to be in.

Divide one of the 8-element lists into two 4-element lists, and determine which half the number needs to be in.

Divide one of the 4-element lists into two 2-element lists, and determine which half the number needs to be in.

Finally, divide one of the 2-element lists into two single-element list. If the number is present in the first place, it will be in one or the other 1-element list.

We divided the 32-element list 5 times to get down to a single number. 32 = 25, and log2 32 = 5. The same reasoning can be generalized to lists with n elements.
 

Related to What Is the Correct Derivation of Binary Search Complexity?

What is binary search complexity?

Binary search complexity refers to the amount of time and resources required to search for an item in a sorted list using the binary search algorithm. It is typically measured in terms of the number of comparisons or steps needed to find the desired item.

How is binary search complexity calculated?

The complexity of binary search is calculated using the formula log2n, where n is the number of items in the list. This means that the worst-case complexity of binary search is O(log n), making it a very efficient algorithm for searching through large amounts of data.

What is the best case scenario for binary search complexity?

The best case scenario for binary search complexity occurs when the desired item is found in the middle of the list, requiring only one comparison. This results in a complexity of O(1), as the number of items in the list does not affect the number of steps needed to find the item.

What is the worst case scenario for binary search complexity?

The worst case scenario for binary search complexity occurs when the desired item is not in the list or is located at either end of the list. This results in a complexity of O(log n), as the algorithm will need to divide the list in half repeatedly until the item is found or determined to not be present.

How does the complexity of binary search compare to other search algorithms?

The complexity of binary search is generally considered to be more efficient than other search algorithms, such as linear search, which has a complexity of O(n). This is because binary search eliminates half of the remaining items in the list with each iteration, resulting in a quicker search process for large lists of data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Linear and Abstract Algebra
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
849
Replies
1
Views
917
Back
Top