question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

  1. Compare x with the middle element.
  2. If x matches with middle element, we return the mid index.
  3. 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.
  4. Else (x is smaller) recur for the left half.
  5. 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:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
harshraj8843commented, Oct 9, 2022

Hey @PravunathSingh

The workflow for adding a new dsa program is as follows :

0reactions
harshraj8843commented, Oct 9, 2022

DSA Program added successfully 🎉

Thanks for your contribution 🤗

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found