What is the time complexity of a program using the binary search algorithm?
The time complexity of a program using the binary search algorithm is "logarithmic level". Binary search is a highly efficient search method. The algorithm complexity is the number of while loops. The time complexity can be expressed as "O(h)=O(log2n)".
The operating environment of this tutorial: Windows 7 system, Dell G3 computer.
The time complexity of a program using the binary search algorithm is "logarithmic level".
Related recommendations: "Programming Learning"
Binary search is also called binary search (Binary Search), which is a more efficient search method. However, binary search requires that the linear table must adopt a sequential storage structure, and the elements in the table must be arranged in order by keywords.
Search process:
First, assuming that the elements in the table are arranged in ascending order, compare the keyword recorded in the middle of the table with the search keyword, if they are equal , then the search is successful; otherwise, the middle position record is used to divide the table into the front and last sub-tables. If the keyword of the middle position record is greater than the search keyword, then the previous sub-table is further searched, otherwise the latter sub-table is further searched. Repeat the above process until a record that meets the conditions is found, making the search successful, or until the subtable does not exist, in which case the search fails.
Algorithm complexity:
The basic idea of binary search is to divide n elements into two roughly equal parts, and compare a[n/2] with x , if x=a[n/2], then x is found and the algorithm terminates; if xa[n/2] , then just search for x in the right half of array a.
The time complexity is the number of while loops.
There are n elements in total,
gradually follows n, n/2, n/4,....n/2^k (the remaining number of elements will be operated next ), where k is the number of loops
Since after you round n/2^k>=1
, you can get n/2^k=1
k=log2n, (based on base 2, the logarithm of n)
So the time complexity can be expressed as O(h)=O(log2n)
as follows Provide a piece of pseudo code for the implementation of binary search:
BinarySearch(max,min,des) mid-<(max+min)/2 while(min<=max) mid=(min+max)/2 if mid=des then return mid elseif mid >des then max=mid-1 else min=mid+1 return max
The half-search method is also called the binary search method. It makes full use of the order relationship between elements and adopts the divide-and-conquer strategy. In the worst case, O (log n) completes the search task. Its basic idea is: (assuming that the array elements are arranged in ascending order) divide n elements into two halves with roughly the same number, take a[n/2] and compare it with the x you want to find, if x=a[n/ 2] then x is found and the algorithm terminates; if x
If you want to read more related articles, please visit PHP Chinese website! ! The above is the detailed content of What is the time complexity of a program using the binary search algorithm?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use C# to write a binary search algorithm. The binary search algorithm is an efficient search algorithm. It finds the position of a specific element in an ordered array with a time complexity of O(logN). In C#, we can write the binary search algorithm through the following steps. Step 1: Prepare data First, we need to prepare a sorted array as the target data for the search. Suppose we want to find the position of a specific element in an array. int[]data={1,3,5,7,9,11,13

A cube root is an integer value that, when multiplied by itself three times in a row, results in the original value. In this article, we will write a Java program that uses binary search to find the cube root of a number. Finding the cube root of a number is an application of the binary search algorithm. In this article, we will discuss in detail how to use binary search to calculate cube roots. Input-output example Example-1:Input:64Output:4 For example, the cube root of 64 is 4, and the output is 4. Example-2:Input:216Output:6 For example, the cube root of 216 is 6, and the output is 6. Binary Search Binary search is an algorithm used to find elements (i.e. keys in a sorted array). Binary Algorithm Working

We know that the binary search method is the most suitable and effective sorting algorithm. This algorithm works on sorted sequences. The algorithm is simple, it just finds the element from the middle, then splits the list into two parts and moves to the left sublist or the right sublist. We know its algorithm. Now we will see how to use the binary search technique in a multi-threaded environment. The number of threads depends on the number of cores present in the system. Let's take a look at the code to get the idea. Example#include<iostream>#defineMAX16#defineMAX_THREAD4usingnamespacestd;//placearr,keyandothervariabl

The C programming language provides two search techniques. They are as follows: Linear Search Binary Search Binary Search This method is only suitable for ordered lists. The given list is divided into two equal parts. The given key is compared to the middle element of the list. Here, three things can happen, as follows: If the middle element matches the keyword, the search will end successfully here. If the middle element is greater than the keyword, the search will take place in the left partition. If the middle element is smaller than the keyword, the search will be performed on the right partition. Input(i/p) - unsorted list of elements, keywords. Output (o/p)-success-if failure to find the keyword-otherwise key=20mid=(low+high)/2 Program 1 The following is the use of binary search in

How to implement binary search algorithm using Python? The binary search algorithm, also known as the binary search algorithm, is an efficient search algorithm. It works on ordered arrays or lists, narrowing down the search by comparing the target value to elements in the middle of the array. The following will introduce how to implement the binary search algorithm in Python and provide specific code examples. Algorithm idea: Compare the target value with the element in the middle of the array; if they are equal, return the element position; if the target value is greater than the element in the middle, then on the right

How to use Java to implement binary search algorithm Binary search algorithm is an efficient search method suitable for sorted arrays. Its basic idea is to continuously narrow the search range, compare the search value with the elements in the middle of the array, and decide whether to continue searching the left half or the right half based on the comparison result until the target element is found or the search range is reduced to empty. Below we will introduce in detail how to implement the binary search algorithm in Java. Step 1: Implement the binary search method publicclassBinarySearch

In this problem, we are given a sorted array of rational numbers. We have to use binary search algorithm to search for a given element of this array of rational numbers without using floating point operations. Rational numbers are numbers expressed in the form p/q, where p and q are both integers. For example, ⅔, ⅕. Binary search is a search technique that finds elements by looking in the middle of an array. Used to find elements in a sorted array of rational numbers using binary search, where floating point operations are not allowed. We will compare the numerator and denominator to find out which element is greater or which element is the one to be found. Example Let's create a program for this, #include<stdio.h>structRational{ &am

PHP algorithm analysis: How to use binary search algorithm to quickly locate elements in an ordered array? Overview: The binary search algorithm is an efficient search algorithm that is suitable for finding specific elements in an ordered array. This article will introduce the principle of binary search algorithm in detail and give PHP code examples. Principle: The binary search algorithm quickly locates the target element by repeatedly reducing the search range by half. The process is as follows: first, narrow the search range to the beginning and end of the array; then, calculate the index of the middle element and compare it with the target element;