search
HomeBackend DevelopmentPHP TutorialPHP data structure (1) binary 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$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]//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
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
mid函数怎么用mid函数怎么用Aug 09, 2023 am 11:08 AM

mid函数用于从一个字符串中截取指定长度的子字符串,具体方法:1、VB中的mid函数,语法“Mid(string, start[, length])”;2、JavaScript中的mid函数,语法“string.slice(startIndex[, endIndex])”;3、Python中的mid函数,语法“string[startIndex:endIndex]”。

使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

简单明了的PHP array_merge_recursive()函数使用方法简单明了的PHP array_merge_recursive()函数使用方法Jun 27, 2023 pm 01:48 PM

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

如何使用PHP中的array_combine函数将两个数组拼成关联数组如何使用PHP中的array_combine函数将两个数组拼成关联数组Jun 26, 2023 pm 01:41 PM

在PHP中,有许多强大的数组函数可以使数组的操作更加方便和快捷。当我们需要将两个数组拼成一个关联数组时,可以使用PHP的array_combine函数来实现这一操作。这个函数实际上是用来将一个数组的键作为另一个数组的值,合并成一个新的关联数组。接下来,我们将会讲解如何使用PHP中的array_combine函数将两个数组拼成关联数组。了解array_comb

mid是什么格式mid是什么格式Apr 23, 2021 pm 03:45 PM

​mid格式是一种音频格式,是由MIDI继承而来,MID文件并不是一段录制好的声音,而是记录声音的信息,然后再告诉声卡如何再现音乐的一组指令;一个mid文件主要包括两部分,分别是标头数据和音轨数据。

CSS 维度属性详解:height 和 widthCSS 维度属性详解:height 和 widthOct 21, 2023 pm 12:42 PM

CSS维度属性详解:height和width在前端开发中,CSS是一种强大的样式定义语言。其中,height和width是两个最基本的维度属性,用于定义元素的高度和宽度。本文将对这两个属性进行详细解析,并提供具体的代码示例。一、height属性height属性用于定义元素的高度。可以使用像素(pixel)、百分比(percentage)或者

PHP array_fill()函数用法详解PHP array_fill()函数用法详解Jun 27, 2023 am 08:42 AM

在PHP编程中,数组是一种非常重要的数据结构,能够轻松地处理大量数据。PHP中提供了许多数组相关的函数,array_fill()就是其中之一。本篇文章将详细介绍array_fill()函数的用法,以及在实际应用中的一些技巧。一、array_fill()函数概述array_fill()函数的作用是创建一个指定长度的、由相同的值组成的数组。具体来说,该函数的语法

The Browser Company推出Arc Search:AI助力,搜索体验再升级The Browser Company推出Arc Search:AI助力,搜索体验再升级Feb 01, 2024 am 09:18 AM

1月31日消息,近日,TheBrowserCompany公司发布了一款名为ArcSearch的全新应用,该应用充分利用AI技术,旨在帮助用户更快速、便捷地获取所需信息。ArcSearch应用的核心特色在于其“Browseforme”功能,这一功能背后由OpenAI等公司的模型提供支持。当用户进行搜索时,该功能能够自动读取至少六个相关网页,并通过AI技术对这些信息进行整合与归纳,最终在一个全新设计的页面中展示给用户。这一页面不仅包含了搜索关键词的相关信息,还将内容划分为不同的部分,使用户能够更清晰

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools