search
HomeBackend DevelopmentPHP TutorialPHP 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:

快速排序,快速排序算法,快速排序 java,c 快速排序,快速排序时间复杂度,java快速排序算法,快速排序法,快速排序c语言,快速排序算法c语

快速排序,快速排序算法,快速排序 java,c 快速排序,快速排序时间复杂度,java快速排序算法,快速排序法,快速排序c语言,快速排序算法c语

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.

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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

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

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

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

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

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

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

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.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

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

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

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.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

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.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

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.