PHP implements bubble sort, selection sort, insertion sort and quick sort quick sort method quick sort c language quick sort algorithm c language

WBOY
Release: 2016-07-29 08:55:14
Original
1374 people have browsed it

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.

"; 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 < $len-1)&&($num>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 < $len-1; $i++) { $temp=$i; for ($j=$i; $j < $len-1; $j++) { if($arr[$temp]>$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 < $len; $i++) { $temp=$arr[$i];//将要插入的数据缓存 for ($j=$i-1; $j >=0; $j--) { if($temp<$arr[$j]){ $arr[$j+1]=$arr[$j];//如果要插入的数据比原数组的最后一个小,则与之交换 $arr[$j]=$temp; }else break; } } return $arr; } p(insertSort($arr)); /** *快速排序法 * @param $arr 排序数组 *思路:选择一个基准元素,通过一趟扫描,将待排序列分成两部分,再对每一部分递归调用自己方法 *最后返回左边排序好的数组和右边排序好的数组和基准元素合并,就是有序数组了。 **/ function quickSort($arr){ $len=count($arr); if($len<=1) return $arr; $baseNum=$arr[0]; $leftArr=$rightArr=array(); //根据基准元素分成两部分 for ($i=1; $i < $len; $i++) { if ($arr[$i]<=$arr[0]) { $leftArr[]=$arr[$i]; }else{ $rightArr[]=$arr[$i]; } } $leftArr=quickSort($leftArr);//递归调用 $rightArr=quickSort($rightArr); return array_merge($leftArr,array($baseNum),$rightArr); } print("**快速排序法**"); p(quickSort($arr)); ?>
Copy after login

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.

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!