0% found this document useful (0 votes)
5 views

Binary Search

Binary Search is an efficient algorithm for finding an element's position in a sorted array, utilizing a divide and conquer approach. It can be implemented using either an iterative or recursive method, both of which involve repeatedly narrowing down the search range based on comparisons with the middle element. Binary Search is widely used in programming libraries and debugging processes.

Uploaded by

anakinalt9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Binary Search

Binary Search is an efficient algorithm for finding an element's position in a sorted array, utilizing a divide and conquer approach. It can be implemented using either an iterative or recursive method, both of which involve repeatedly narrowing down the search range based on comparisons with the middle element. Binary Search is widely used in programming libraries and debugging processes.

Uploaded by

anakinalt9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Binary Search

Binary Search is a searching algorithm for finding an element's position in


a sorted array.

In this approach, the element is always searched in the middle of a portion


of an array.

Binary search can be implemented only on a sorted list of items. If the


elements are not sorted already, we need to sort them first.

Binary Search Working

Binary Search Algorithm can be implemented in two ways which are


discussed below.

1. Iterative Method

2. Recursive Method

The recursive method follows the divide and conquer approach.

The general steps for both methods are discussed below.

1. The array in which searching is to be performed is:

Initial array
Let x = 4 be the element to be searched.

2. Set two pointers low and high at the lowest and the highest positions
respectively. Setting pointers
3. Find the middle position mid of the array ie. mid = (low +
high)/2 and arr[mid] = 6.Mid element.

4. If x == arr[mid], then return mid. Else, compare the element to be


searched with arr[mid].

5. If x > arr[mid], compare x with the middle element of the elements


on the right side of arr[mid]. This is done by setting low to low = mid
+ 1.

6. Else, compare x with the middle element of the elements on the left
side of arr[mid]. This is done by setting high to high = mid - 1.

Finding mid element

7. Repeat steps 3 to 6 until low meets high.

Mid Element

8. x = 4 is found. Found
Mid Element Found

// Binary Search in C (Iterative Method)

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {

// Repeat until the pointers low and high meet each other

while (low <= high) {

int mid = low + (high - low) / 2;

if (x == array[mid])

return mid;

if (x > array[mid])

low = mid + 1;

else

high = mid - 1;

return -1;

int main(void) {

int array[] = {3, 4, 5, 6, 7, 8, 9};

int n = sizeof(array) / sizeof(array[0]);


int x = 4;

int result = binarySearch(array, x, 0, n - 1);

if (result == -1)

printf("Not found");

else

printf("Element is found at index %d", result);

return 0;

OutPut : Element is found at index 1

(Recursive Method)

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {

if (high >= low) {

int mid = low + (high - low) / 2;

// If found at mid, then return it

if (x == array[mid])

return mid;

// Search the right half

if (x > array[mid])

return binarySearch(array, x, mid + 1, high);


// Search the left half

return binarySearch(array, x, low, mid - 1);

return -1;

int main(void) {

int array[] = {3, 4, 5, 6, 7, 8, 9};

int n = sizeof(array) / sizeof(array[0]);

int x = 4;

int result = binarySearch(array, x, 0, n - 1);

if (result == -1)

printf("Not found");

else

printf("Element is found at index %d", result);

Output:

Element is found at index 1

Binary Search Applications

 In libraries of Java, .Net, C++ STL

 While debugging, the binary search is used to pinpoint the


place where the error happens.

You might also like