Binary Search | Algorithms - Computer Science Engineering (CSE) PDF Download

Introduction

Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
A simple approach is to do a linear search. The time complexity of the above algorithm is O(n). Another approach to perform the same task is using Binary Search.
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
Example :
Binary Search | Algorithms - Computer Science Engineering (CSE) The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
We basically ignore half of the elements just after one comparison.

  1. Compare x with the middle element.
  2. If x matches with the middle element, we return the mid index.
  3. Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half.
  4. Else (x is smaller) recur for the left half.

Recursive implementation of Binary Search

C++
Binary Search | Algorithms - Computer Science Engineering (CSE) 
Binary Search | Algorithms - Computer Science Engineering (CSE)

C
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Java
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Python3
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

C#
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

PHP
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Javascript
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Output:
Element is present at index 3
Here you can create a check function for easier implementation.
Here is recursive implementation with check function which I feel is a much easier implementation:

C++
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Iterative implementation of Binary Search

C++
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

C
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Java
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Python3
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

C#
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

PHP

Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Javascript
Binary Search | Algorithms - Computer Science Engineering (CSE)
Binary Search | Algorithms - Computer Science Engineering (CSE)

Output:
Element is present at index 3
Time Complexity: The time complexity of Binary Search can be written as
T(n) = T(n / 2) + c
The above recurrence can be solved either using the Recurrence Tree method or Master method. It falls in case II of the Master Method and the solution of the recurrence is Theta(Logn).

Auxiliary Space: O(1) in case of iterative implementation. In the case of recursive implementation, O(Logn) recursion call stack space.

Algorithmic Paradigm: Decrease and Conquer.

Note: Here we are using
int mid = low + (high – low) / 2;
Maybe, you wonder why we are calculating the middle index this way, we can simply add the lower and higher index and divide it by 2.
int mid = (low + high)/2;
But if we calculate the middle index like this means our code is not 100% correct, it contains bugs.
That is, it fails for larger values of int variables low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive int value(231 – 1 ).
The sum overflows to a negative value and the value stays negative when divided by 2. In java, it throws Array Index Out Of Bound Exception.
int mid = low + (high – low) / 2;
So it’s better to use it like this. This bug applies equally to merge sort and other divide and conquer algorithms.

The document Binary Search | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Binary Search - Algorithms - Computer Science Engineering (CSE)

1. What is binary search in computer science engineering?
Ans. Binary search is a commonly used algorithm in computer science engineering to search for a specific target value within a sorted array or list. It works by repeatedly dividing the search space in half until the target value is found or determined to be not present. This algorithm is efficient and has a time complexity of O(log n), making it a popular choice for searching in large datasets.
2. How does binary search work?
Ans. Binary search works by dividing the search space in half at each step. It compares the target value with the middle element of the array. If the target value matches the middle element, the search is successful. If the target value is smaller, the search continues in the lower half of the array. If the target value is larger, the search continues in the upper half of the array. This process is repeated until the target value is found or the search space is empty.
3. What are the advantages of using binary search?
Ans. There are several advantages of using binary search: - It has a time complexity of O(log n), making it efficient for searching in large datasets. - It can be implemented iteratively or recursively, providing flexibility in coding. - It guarantees to find the target value in a sorted array if it exists. - It reduces the number of comparisons needed compared to linear search, especially for large datasets. - It can be modified to find the first or last occurrence of a target value in a sorted array.
4. Can binary search only be used for searching in arrays?
Ans. No, binary search can be used for searching in other data structures as well, not just arrays. It can be applied to search in sorted linked lists, binary trees, and other sorted data structures. The key requirement is that the data structure should support random access or efficient access to the middle element, which is essential for the binary search algorithm.
5. Are there any limitations of binary search?
Ans. Yes, binary search has a few limitations: - The array or data structure must be sorted beforehand for binary search to work correctly. - It requires random access or efficient access to the middle element, which may not be available in all data structures. - It is not suitable for searching in unsorted arrays or data structures. - It may not be the best choice if the data undergoes frequent insertions or deletions, as maintaining the sorted order can be costly. - Binary search may not be efficient for small datasets or datasets with a high degree of randomness, as the overhead of dividing the search space may outweigh the benefits of the algorithm.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Binary Search | Algorithms - Computer Science Engineering (CSE)

,

Sample Paper

,

Objective type Questions

,

shortcuts and tricks

,

Summary

,

Free

,

video lectures

,

study material

,

Viva Questions

,

Exam

,

Binary Search | Algorithms - Computer Science Engineering (CSE)

,

Semester Notes

,

past year papers

,

mock tests for examination

,

MCQs

,

ppt

,

Extra Questions

,

Previous Year Questions with Solutions

,

practice quizzes

,

Binary Search | Algorithms - Computer Science Engineering (CSE)

,

pdf

,

Important questions

;