


PHP implements bubble sort, selection sort, insertion sort and quick sort quick sort method quick sort c language quick sort algorithm c language
I have read the four basic sorting methods in C language before when I was studying data structure by myself. I have almost forgotten the C language. Recently, when I had time, I re-wrote the four sorting methods in PHP to review the increasingly unfamiliar algorithms. . Paste the code directly.
<?php /** * 打印数据,用于调试 * @param var 打印对象 */ function p($var){ echo "<pre class="brush:php;toolbar:false">"; print_r($var); echo ""; } $arr=array(33,11,22,66,55,44,88,99,77); printf("**原数组**"); p($arr); /** *冒泡排序法 * @param $arr 排序数组 *思路:和相邻的数字对比,每次对比如果左边比右边大则交换位置。 *两个节点,一个方向:两次循环次数,冒泡方向(即$j的初值和终止条件) *num的作用是做了优化,一旦循环没有交换则冒泡已完成 **/ function bubbleSort($arr){ print("**冒泡排序法**"); $len=count($arr); $temp=0; $num=1; for ($i=0; ($i 0); $i++) { $num=0; for ($j=$len-1; $j>$i; $j--) { if($arr[$j]>$arr[$j-1]){ continue; }else{ //冒泡交换 $temp=$arr[$j-1]; $arr[$j-1]=$arr[$j];; $arr[$j]=$temp; $num++; } } } return $arr; } p(bubbleSort($arr)); /** *选择排序法 * @param $arr 排序数组 *思路:每一轮循环,找出最小的数字,把其下标保存到最左方数字 *而找出最小数字的方法是:和右方数字比较,若右方比较小,则保存其下标,再将此下标对应的值和 *下一个右方数字比较直到所有右方数字比较一遍,再将此最小值存放在最左方数字 **/ function selectSort($arr){ print("**选择排序法**"); $len=count($arr); $buff=0; for ($i=0; $i $arr[$j+1]){ $temp=$j+1;//若右方数字更小,则保存其下标,用来跟后面的数据比较 } } //得到最小的数字下标 if($temp!=$i){ //如果下标不是$i则交换,不然就没必要 $buff=$arr[$temp]; $arr[$temp]=$arr[$i]; $arr[$i]=$buff; } } return $arr; } p(selectSort($arr)); /** *插入排序法 * @param $arr 排序数组 *思路:假设前面的数已经是排好顺序的,现在要把第n个数插到前面的有序数中。即把第二个数据插入到 *第一个数据之中,使其形成一个有序数组,然后再讲第三个数插入到前面两个数组成的有序数组中,形成 *有序数组,如此反复最后完成排序 **/ function insertSort($arr){ print("**插入排序法**"); $len=count($arr); for ($i=1; $i =0; $j--) { if($temp
Running result:
The above introduces the implementation of bubble sort, selection sort, insertion sort and quick sort in PHP, including quick sort and insertion sort. I hope it will be helpful to friends who are interested in PHP tutorials.

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
