Table of Contents
PHP 四种基本排序算法的代码实现(1)
Home php教程 php手册 PHP 四种基本排序算法的代码实现(1)

PHP 四种基本排序算法的代码实现(1)

Jun 13, 2016 am 09:11 AM
php Code

PHP 四种基本排序算法的代码实现(1)

许多人都说算法是程序的核心,算法的好坏决定了程序的质量。作为一个初级phper,虽然很少接触到算法方面的东西。但是对于基本的排序算法还是应该掌握的,它是程序开发的必备工具。这里介绍冒泡排序,插入排序,选择排序,快速排序四种基本算法,分析一下算法的思路。

前提:分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序。

$arr(1,43,54,62,21,66,32,78,36,76,39);

1. 冒泡排序

思路分析:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即,每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

<ol class="dp-j"><li class="alt"><span><span>$arr=array(</span><span class="number">1</span><span>,</span><span class="number">43</span><span>,</span><span class="number">54</span><span>,</span><span class="number">62</span><span>,</span><span class="number">21</span><span>,</span><span class="number">66</span><span>,</span><span class="number">32</span><span>,</span><span class="number">78</span><span>,</span><span class="number">36</span><span>,</span><span class="number">76</span><span>,</span><span class="number">39</span><span>);   </span></span></li><li><span>function bubbleSort($arr) </span></li><li class="alt"><span>{   </span></li><li><span>  $len=count($arr); </span></li><li class="alt"><span>  <span class="comment">//该层循环控制 需要冒泡的轮数</span><span> </span></span></li><li><span>  <span class="keyword">for</span><span>($i=</span><span class="number">1</span><span>;$i<$len;$i++) </span></span></li><li class="alt"><span>  { <span class="comment">//该层循环用来控制每轮 冒出一个数 需要比较的次数</span><span> </span></span></li><li><span>    <span class="keyword">for</span><span>($k=</span><span class="number">0</span><span>;$k<$len-$i;$k++) </span></span></li><li class="alt"><span>    { </span></li><li><span>       <span class="keyword">if</span><span>($arr[$k]>$arr[$k+</span><span class="number">1</span><span>]) </span></span></li><li class="alt"><span>        { </span></li><li><span>            $tmp=$arr[$k+<span class="number">1</span><span>]; </span></span></li><li class="alt"><span>            $arr[$k+<span class="number">1</span><span>]=$arr[$k]; </span></span></li><li><span>            $arr[$k]=$tmp; </span></li><li class="alt"><span>        } </span></li><li><span>    } </span></li><li class="alt"><span>  } </span></li><li><span>  <span class="keyword">return</span><span> $arr; </span></span></li><li class="alt"><span>} </span></li></ol>
Copy after login

2. 选择排序

思路分析:在要排序的一组数中,选出最小的一个数与第一个位置的数交换。然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

<ol class="dp-j"><li class="alt"><span><span>function selectSort($arr) { </span></span></li><li><span><span class="comment">//双重循环完成,外层控制轮数,内层控制比较次数</span><span> </span></span></li><li class="alt"><span>$len=count($arr); </span></li><li><span><span class="keyword">for</span><span>($i=</span><span class="number">0</span><span>; $i<$len-</span><span class="number">1</span><span>; $i++) { </span></span></li><li class="alt"><span><span class="comment">//先假设最小的值的位置</span><span> </span></span></li><li><span>$p = $i; </span></li><li class="alt"><span> </span></li><li><span><span class="keyword">for</span><span>($j=$i+</span><span class="number">1</span><span>; $j<$len; $j++) { </span></span></li><li class="alt"><span><span class="comment">//$arr[$p] 是当前已知的最小值</span><span> </span></span></li><li><span><span class="keyword">if</span><span>($arr[$p] > $arr[$j]) { </span></span></li><li class="alt"><span><span class="comment">//比较,发现更小的,记录下最小值的位置;并且在下次比较时采用已知的最小值进行比较。</span><span> </span></span></li><li><span>$p = $j; </span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="comment">//已经确定了当前的最小值的位置,保存到$p中。如果发现最小值的位置与当前假设的位置$i不同,则位置互换即可。</span><span> </span></span></li><li><span><span class="keyword">if</span><span>($p != $i) { </span></span></li><li class="alt"><span>$tmp = $arr[$p]; </span></li><li><span>$arr[$p] = $arr[$i]; </span></li><li class="alt"><span>$arr[$i] = $tmp; </span></li><li><span>} </span></li><li class="alt"><span>} </span></li><li><span><span class="comment">//返回最终结果</span><span> </span></span></li><li class="alt"><span><span class="keyword">return</span><span> $arr; </span></span></li><li><span>} </span></li></ol>
Copy after login




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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles