Home Backend Development PHP Tutorial PHP data structure (1) binary search

PHP data structure (1) binary search

Aug 08, 2016 am 09:32 AM
array height mid search

The basic idea of ​​binary search is to compare the middle value of an ordered array with the value you are looking for. When the value you are looking for is greater than the middle value of the array, it means all the values ​​before the middle value of the ordered array. are all less than the value to be searched, so you can exclude all values ​​before the middle value of the array, and then continue to search for the required value from the middle value of the array to the value at the end of the array. The code is implemented as follows:

//Binary search
function bin_search($array,$search){
$low=0;
$height=count($array)-1;//Get Array length

while($low<=$height){
$mid=floor(($low+$height)/2);//Get the middle number and cast it to floor type, Prevent errors
if($array[$mid]==$search){
return $mid+1;//Return the found serial number
}else if($array[ $mid]<$search){
//When the middle value is less than the checked value, the values ​​to the left of $mid are all less than $search. At this time, $mid should be assigned to $low
$ low=$mid+1;
}else if($array[$mid]>$search){
//At this time, it means that the middle value is greater than the checked value, then all the values ​​to the right of $mid are is greater than $search, at this time $mid should be assigned to $height
$height=$mid-1;
}
return "Search failed";//The search failed, the item does not exist in the array Value

}

}
$arr=array(1,4,6,33,75,88,89,93);
echo bin_search($arr, 33);
echo bin_search($arr,66);
?>

The above introduces the PHP data structure (1) binary search, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use mid function How to use mid function Aug 09, 2023 am 11:08 AM

The mid function is used to intercept a substring of a specified length from a string. The specific method is: 1. The mid function in VB, the syntax is "Mid(string, start[, length])"; 2. The mid function in JavaScript, Syntax "string.slice(startIndex[, endIndex])"; 3. Mid function in Python, syntax "string[startIndex:endIndex]".

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

The Browser Company launches Arc Search: AI-assisted, upgraded search experience The Browser Company launches Arc Search: AI-assisted, upgraded search experience Feb 01, 2024 am 09:18 AM

According to news on January 31, TheBrowserCompany recently released a new application called ArcSearch, which makes full use of AI technology and aims to help users obtain the information they need more quickly and conveniently. The core feature of the ArcSearch application is its "Browseforme" function, which is powered by models from companies such as OpenAI. When a user searches, this function can automatically read at least six related web pages, integrate and summarize this information through AI technology, and finally display it to the user on a newly designed page. This page not only contains information related to the search keywords, but also divides the content into different parts to make it clearer for users.

Detailed explanation of CSS dimension properties: height and width Detailed explanation of CSS dimension properties: height and width Oct 21, 2023 pm 12:42 PM

Detailed explanation of CSS dimension properties: height and width In front-end development, CSS is a powerful style definition language. Among them, height and width are the two most basic dimension attributes, used to define the height and width of the element. This article will analyze these two properties in detail and provide specific code examples. 1. Height attribute The height attribute is used to define the height of an element. You can use pixel, percentage or

What is the format of mid? What is the format of mid? Apr 23, 2021 pm 03:45 PM

The mid format is an audio format that is inherited from MIDI. The MID file is not a recorded sound, but a set of instructions that records the sound information and then tells the sound card how to reproduce the music; a mid file mainly includes The two parts are header data and audio track data.

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

See all articles