JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

Binary Search

Author:JIYIK Last Updated:2025/03/19 Views:

Binary search is the most popular and efficient search algorithm. In fact, it is the fastest search algorithm. Like jump sort, it also requires the array to be sorted. It is based on a divide and conquer approach where we divide the array into two halves and then compare the item we are searching for with the item in the middle. If the middle item matches, we return the index of the middle element; otherwise, we move to the left or right half based on the value of the item.

Binary Search Algorithm

Suppose we have an unsorted array A[]containing nelements and we want to find an element X.

  • Let lo be 0 and hi be n - 1.
  • I'm sorry.
    • 设 mid=lo + (hi - lo)/2。
    • If A[mid] == X return mid.
    • If A[mid] < X, then lo = mid+1.
    • else if A[mid] > X then hi= mid-1。
  • If the element is not found, -1 is returned.

Binary Search Example

  1. Let lo be 0 and hi be 8.
  2. Calculate mid to be 4. Since A[mid]<X, set lo=4+1=5.
  3. Calculate mid to be 6. Since A[mid]<X, set lo=6+1=7.
  4. Calculate mid to be 7. Since A[mid]=8, 7 is returned.

Suppose we have an array , (1,2,3,4,5,6,7,8,9)and we want to find X - 8.

Implementation of Binary Search Algorithm

#include <bits/stdc++.h>
using namespace std;

int binarySearch(int arr[], int lo, int hi, int x)
{
    while (lo <= hi) {
        int m = lo + (hi - lo) / 2;
        if (arr[m] == x)
            return m;
        if (arr[m] < x)
            lo = m + 1;
        else
            hi = m - 1;
    }
    return -1;
}

int main(void)
{
    int n = 9;
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int x = 8;
    int result = binarySearch(arr, 0, n - 1, x);
    if (result == -1) {
        cout << "Element not found !!";
    }
    else cout << "Element found at index " << result;
    return 0;
}

Complexity of Binary Search Algorithm

Time Complexity

  • Average situation

When we do a binary search, we search half and discard the other half, reducing the size of the array by half each time.

The expression for time complexity is given by recursion.

T(n) = T(n/2) + k , k is a constant.

The result of this recursion gives logna time complexity of O(logn). It is faster than both linear search and jump search.

  • Best Case

The best case is when the middle element is the one we are searching for and it is returned in the first iteration. The time complexity for the best case is O(1).

  • Worst case scenario

The worst case time complexity is the same as the average case time complexity. The worst case time complexity is O(logn).

Space complexity

In case of iterative execution, the space complexity of this algorithm is O(1)since it does not require any data structures except temporary variables.

In case of a recursive implementation, the space complexity is 1/2 due to the space required for the recursive call stack O(logn).

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert a binary tree to a binary search tree

Publish Date:2025/03/18 Views:53 Category:ALGORITHM

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node

In-order descendants in a binary search tree

Publish Date:2025/03/18 Views:108 Category:ALGORITHM

The in-order descendant of a binary tree is the next node in the in-order traversal of the binary tree. So, for the last node in the tree, it is NULL . Since the in-order traversal of a binary search tree is a sorted array. The node with th

Binary Search Tree Deletion

Publish Date:2025/03/18 Views:94 Category:ALGORITHM

In the article Binary Search Trees: Searching and Inserting , we discussed how to insert an element into a binary search tree and how to search for a value in a binary search tree. In this article, we will discuss how to delete a node from

Binary Search Tree Check

Publish Date:2025/03/18 Views:79 Category:ALGORITHM

A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left children and right children. For a binary tree to be a BST, it must satisfy the following pr

Iterative insertion into a binary search tree

Publish Date:2025/03/18 Views:159 Category:ALGORITHM

In the previous article Binary Search Tree , we discussed the recursive method to insert a node in BST. In this article, we will discuss the iterative method to insert a node in BST. It is better than the recursive method because the iterat

Binary Search Tree

Publish Date:2025/03/18 Views:181 Category:ALGORITHM

A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary

使用 JavaScript 进行二叉搜索

Publish Date:2024/03/23 Views:157 Category:JavaScript

在本文中展示了我们如何使用 JavaScript 搜索元素,或者我们也可以将其称为二叉搜索方法。我们将通过示例解释两种方法。

C++ 二叉搜索树析构函数

Publish Date:2023/08/31 Views:179 Category:C++

本文将讨论使用 C++ 中的 delete 关键字为二叉搜索树创建析构函数。C++ 二叉搜索树析构函数

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial