search
Homephp教程php手册实例详解php中的四种排序算法 (附代码)

这篇文章主要介绍了php中的四种排序算法,有一定的参考价值,感兴趣的朋友可以一下,希望可以帮助到你!

(1)快速排序

原理:选择数组中的一个数(通常是第一个数)作为基准,在排序过程中,定义两个数组。第一次比较时,将大于它的数放在右边数组中,将小于它的数放在左边数组中。接下来采用递归的方式将左右两边的数组做同样处理,直到数组中只剩一个元素的时候结束递归,最后将数组合并。

function quick_sort($arr) {
      if(!is_array($arr)) return false;
      $length = count($arr);
      //如果数组长度为1的话,直接返回数组
      if ($length<=1) return $arr;
      //数组元素有多个,则定义两个数组
      $left = $right = array();
      //使用for循环进行遍历,把数组的第一个元素当成比较的对象
      for($i=1;$i<$length;$i++) {
            if($arr[$i]<$arr[0]) {
                  $left[] = $arr[$i];
            } else {
                  $right[] = $arr[$i];
            }
      }
      //递归调用
      $left = quick_sort($left);
      $right = quick_sort($right);
      //合并结果
      return array_merge($left,array($arr[0]),$right);
}

(2)冒泡排序

原理:数组中相邻的两个元素依次进行比较,大的数字排在后面,小的数字放在前面,这样保证每轮比较放到后面的都是此次比较最大的数字。

function bubble_sort($arr) {
      if (!is_array($arr)) return false;
      if(count($arr)<=1) return $arr;
      $length = count($arr);
      //该层循环控制需要冒泡的次数
      for ($i=1;$i<$length;$i++) {
            //该层循环控制冒出一个数需要比较的次数,每一次比较最大的数一定会被排在最右边
            for ($k=0;$k<$length-$i;$k++) {
                  if ($arr[$k]>$arr[$k+1]) {
                        $temp = $arr[$k];
                        $arr[$k] = $arr[$k+1];
                        $arr[$k+1] = $temp; //交换数据时需要设置一个临时变量$temp
                  }
            }
      }
      return $arr;
}

(3)插入排序

原理:在插入第n个数的时候,我们假设n之前的数字全部排序完毕,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。如此反复循环,直到全部排好顺序。

function insert_sort ($arr) {
      $length = count($arr);
      //外层循环控制要插入的是第几个数
      for ($i=1;$i<$length;$i++) {
            $tmp = $arr[$i];
            //内层循环控制比较次数
            for ($k=$i-1;$k>=0;$k--) {
                  if ($tmp<$arr[$k]) {
                        //当要插入的数字比前面序列数字小时,先将前面前后数字进行交换,再将数字插入
                        $arr[$k+1] = $arr[$k];
                        $arr[$k] = $tmp;
                  } else {
                        break;
                  }
            }
      }
      return $arr;
}

(4)选择排序

原理:第一次,将数组中最小的数字拿出来放在第一位,第二次,将数组中第二小的数字拿出来放在第二位,以此类推,直至排序完毕。

function select_sort ($arr) {
      //两层循环,外层控制轮数,内层控制比较次数
      $length = count($arr);
      for ($i=0;$i<$length-1;$i++) {
            $p = $i;  //假设一个最小值
            for($k=$i+1;$k<$length;$k++) {
                  if ($arr[$p]>$arr[$k]) {
                        $temp = $arr[$p];
                        $arr[$p] = $arr[$k];
                        $arr[$k] = $temp;
                  }
            }
      }
      return $arr;
}

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Statement
This article is reproduced at:CSDN博客. If there is any infringement, please contact admin@php.cn delete

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 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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.