


PHP insertion sort method to implement array sorting example, array example_PHP tutorial
php insertion sort method to implement array sorting example, array example
The example in this article describes how to implement array sorting using PHP insertion sort. Share it with everyone for your reference. The specific analysis is as follows:
The basic idea of insertion sorting method: also use a case to illustrate, still use $arr = array(2,6,3,9), sort from large to small.
Implementation principle: Suppose (without actually creating) there is an ordered array $arr = array(2), use $arr[1]=6 to compare with it, if 6>2, use $arr[ 0] is then moved to position $arr[1], and 6 is inserted into position $arr[0]. Then, $arr[2]=3 is compared with $arr[1]=2, 3>2, then $arr[1]=2 continues to move to the $arr[2] position, the original $arr[3]=3 Insert it into the $arr[1] position, and then continue to insert 9 into the closing position. The final result is (9,6,3,2). Sorting can also be achieved after count($arr)-1 large loops.
Code pattern analysis:
The first big loop: $[1] and $[0] ratio;
The second big cycle: $[2] versus $[1], $[1] versus $[0];
The third big cycle: $[3] versus $[2], $[2] versus $[1], $[1] versus $[0];
PHP code uses function encapsulation for ease of use
<?php function insertSort(&$arr){ for($i=1;$i<count($arr);$i++){ //$insertVal是准备插入的数 for($j=$i;$j>0;$j--){ if($arr[$j]>$arr[$j-1]){ $insertVal = $arr[$j]; $arr[$j] = $arr[$j-1]; $arr[$j-1] = $insertVal; } } } } $myarr = array(2,6,3,9); insertSort($myarr); echo "<pre class="brush:php;toolbar:false">"; print_r($myarr); ?>
Code implementation analysis:
The first big cycle:
$i=1 array(2,6,3,9)
$j=1 Execute the ratio of 6 and 2: $arr[1]=2;$arr[0]=6, get (6,2,3,9)
Second cycle:
$i=2 array(6,2,3,9)
$j=2 performs the ratio of 3 and 2: it becomes $arr[2]=2, $arr[1]=3, and we get (6,3,2,9)
$j--,$j=1 Execute $arr[1] and $arr[0] ratio: the condition is not true
The third cycle:
$i=3 array(6,3,2,9)
$j=3 performs the ratio of 9 and 2: it becomes $arr[3]=2, $arr[2]=9, and we get (6,3,9,2)
$j--,$j=2 Execute the ratio of 9 and 3: it becomes $arr[2]=3, $arr[1]=9, and get (6,9,3,2)
$j--,$j=1 perform the ratio of 9 and 6: it becomes $arr[1]=5, $arr[0]=9, and we get (9,6,3,2)
I hope this article will be helpful to everyone’s PHP programming design.

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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