Binary Search
See original GitHub issueIssue Description
Description
Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.
Input : 10 20 30 40 50 60 70 80 90 100
Target : 30
Output : 2
Steps to implement Binary Search
- Compare x with the middle element.
- If x matches with middle element, we return the mid index.
- Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
- Else (x is smaller) recur for the left half.
- End
10 20 30 40 50 60 70 80 90 100
------------------------------
^
30 => Not Found
10 20 30 40 50 60 70 80 90 100
--------------
^
30 => Found
Issue Analytics
- State:
- Created 5 months ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Binary Search
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary...
Read more >Binary search algorithm
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position...
Read more >Binary Search (With Code)
Binary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched...
Read more >Binary search (article) | Algorithms
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half...
Read more >Data Structure and Algorithms Binary Search
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @PravunathSingh
The workflow for adding a new dsa program is as follows :
submit-dsa
labelDSA Program added successfully 🎉
Thanks for your contribution 🤗