Examples of common algorithms and data structures in PHP
<?php /** * Created by PhpStorm. * User: qishou * Date: 15-8-2 * Time: 上午9:12 */ header("content-type:text/html;charset=utf-8"); $arr = array(3,5,8,4,9,6,1,7,2); echo implode(" ",$arr)."<br/>"; //--------------------------------------- // 常用排序算法 //--------------------------------------- //冒泡排序 function BubbleSort($arr){ $length = count($arr); if($length<=1){ return $arr; } for($i=0;$i<$length;$i++){ for($j=$length;$j>$i;$j--){ if($arr[$j]<$arr[$j-1]){ $tmp = $arr[$j]; $arr[$j] = $arr[$j-1]; $arr[$j-1] = $tmp; } } } return $arr; } echo '冒泡排序:'; echo implode(' ',BubbleSort($arr))."<br/>"; //快速排序 function QSort($arr){ $length = count($arr); if($length <=1){ return $arr; } $pivot = $arr[0];//枢轴 $left_arr = array(); $right_arr = array(); for($i=1;$i<$length;$i++){//注意$i从1开始0是枢轴 if($arr[$i]<=$pivot){ $left_arr[] = $arr[$i]; }else{ $right_arr[] = $arr[$i]; } } $left_arr = QSort($left_arr);//递归排序左半部分 $right_arr = QSort($right_arr);//递归排序右半部份 return array_merge($left_arr,array($pivot),$right_arr);//合并左半部分、枢轴、右半部分 } echo "快速排序:"; echo implode(' ',QSort($arr))."<br/>"; //选择排序(不稳定) function SelectSort($arr){ $length = count($arr); if($length<=1){ return $arr; } for($i=0;$i<$length;$i++){ $min = $i; for($j=$i+1;$j<$length;$j++){ if($arr[$j]<$arr[$min]){ $min = $j; } } if($i != $min){ $tmp = $arr[$i]; $arr[$i] = $arr[$min]; $arr[$min] = $tmp; } } return $arr; } echo "选择排序:"; echo implode(' ',SelectSort($arr))."<br/>"; //插入排序 function InsertSort($arr){ $length = count($arr); if($length <=1){ return $arr; } for($i=1;$i<$length;$i++){ $x = $arr[$i]; $j = $i-1; while($x<$arr[$j] && $j>=0){ $arr[$j+1] = $arr[$j]; $j--; } $arr[$j+1] = $x; } return $arr; } echo '插入排序:'; echo implode(' ',InsertSort($arr))."<br/>"; //--------------------------------------- // 常用查找算法 //--------------------------------------- //二分查找 function binary_search($arr,$low,$high,$key){ while($low<=$high){ $mid = intval(($low+$high)/2); if($key == $arr[$mid]){ return $mid+1; }elseif($key<$arr[$mid]){ $high = $mid-1; }elseif($key>$arr[$mid]){ $low = $mid+1; } } return -1; } $key = 6; echo "二分查找{$key}的位置:"; echo binary_search(QSort($arr),0,8,$key); //顺序查找 function SqSearch($arr,$key){ $length = count($arr); for($i=0;$i<$length;$i++){ if($key == $arr[$i]){ return $i+1; } } return -1; } $key = 8; echo "<br/>顺序常规查找{$key}的位置:"; echo SqSearch($arr,$key); //--------------------------------------- // 常用数据结构 //--------------------------------------- //线性表的删除(数组实现) function delete_array_element($arr,$pos){ $length = count($arr); if($pos<1 || $pos>$length){ return "删除位置出错!"; } for($i=$pos-1;$i<$length-1;$i++){ $arr[$i] = $arr[$i+1]; } array_pop($arr); return $arr; } $pos = 3; echo "<br/>除第{$pos}位置上的元素后:"; echo implode(' ',delete_array_element($arr,$pos))."<br/>"; /** * Class Node * PHP模拟链表的基本操作 */ class Node{ public $data = ''; public $next = null; } //初始化 function init($linkList){ $linkList->data = 0; //用来记录链表长度 $linkList->next = null; } //头插法创建链表 function createHead(&$linkList,$length){ for($i=0;$i<$length;$i++){ $newNode = new Node(); $newNode->data = $i; $newNode->next = $linkList->next;//因为PHP中对象本身就是引用所以不用再可用“&” $linkList->next = $newNode; $linkList->data++; } } //尾插法创建链表 function createTail(&$linkList,$length){ $r = $linkList; for($i=0;$i<$length;$i++){ $newNode = new Node(); $newNode->data = $i; $newNode->next = $r->next; $r->next = $newNode; $r = $newNode; $linkList->data++; } } //在指定位置插入指定元素 function insert($linkList,$pos,$elem){ if($pos<1 && $pos>$linkList->data+1){ echo "插入位置错误!"; } $p = $linkList; for($i=1;$i<$pos;$i++){ $p = $p->next; } $newNode = new Node(); $newNode->data = $elem; $newNode->next = $p->next; $p->next = $newNode; } //删除指定位置的元素 function delete($linkList,$pos){ if($pos<1 && $pos>$linkList->data+1){ echo "位置不存在!"; } $p = $linkList; for($i=1;$i<$pos;$i++){ $p = $p->next; } $q = $p->next; $p->next = $q->next; unset($q); $linkList->data--; } //输出链表数据 function show($linkList){ $p = $linkList->next; while($p!=null){ echo $p->data." "; $p = $p->next; } echo '<br/>'; } $linkList = new Node(); init($linkList);//初始化 createTail($linkList,10);//尾插法创建链表 show($linkList);//打印出链表 insert($linkList,3,'a');//插入 show($linkList); delete($linkList,3);//删除 show($linkList); /** * Class Stack * 用PHP模拟顺序栈的基本操作 */ class Stack{ //用默认值直接初始化栈了,也可用构造方法初始化栈 private $top = -1; private $maxSize = 5; private $stack = array(); //入栈 public function push($elem){ if($this->top >= $this->maxSize-1){ echo "栈已满!<br/>"; return; } $this->top++; $this->stack[$this->top] = $elem; } //出栈 public function pop(){ if($this->top == -1){ echo "栈是空的!"; return ; } $elem = $this->stack[$this->top]; unset($this->stack[$this->top]); $this->top--; return $elem; } //打印栈 public function show(){ for($i=$this->top;$i>=0;$i--){ echo $this->stack[$i]." "; } echo "<br/>"; } } $stack = new Stack(); $stack->push(3); $stack->push(5); $stack->push(8); $stack->push(7); $stack->push(9); $stack->push(2); $stack->show(); $stack->pop(); $stack->pop(); $stack->pop(); $stack->show();
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above has introduced examples of commonly used algorithms and data structures in PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

The length function is used to return the number of characters or bytes in a specified string. It can be used to calculate the length of a string for operation and judgment when querying and processing string data. It should be noted that the length function counts the number of characters in the string, not the number of bytes. For multibyte character sets, a character may consist of multiple bytes. Therefore, the length function counts multibyte characters as one character when calculating the string length.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers
