Home Backend Development PHP Tutorial Sorting algorithm: PHP version quick sort, bubble sort_PHP tutorial

Sorting algorithm: PHP version quick sort, bubble sort_PHP tutorial

Jul 13, 2016 am 10:34 AM
php Bubble Sort Quick sort Sorting Algorithm

1. Quick sort

1. Introduction
Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this situation is uncommon. In fact, quicksort is often significantly faster than other Ο(n log n) algorithms because its inner loop can be implemented efficiently on most architectures.
Quick sort uses the divide and conquer strategy to divide a sequence (list) into two sub-series (sub-lists).
2. Steps
Pick an element from the sequence, called the "pivot",
Reorder the sequence, all elements smaller than the pivot value are placed in front of the pivot, and all elements smaller than the pivot value are placed in front of the pivot. The one with the larger value is placed behind the base (the same number can go to either side). After this partition exits, the base is in the middle of the sequence. This is called a partition operation.
Recursively sort the subarray of elements smaller than the base value and the subarray of elements greater than the base value.
3. Code implementation

Copy code The code is as follows:
function quickSort(array $array)
{
$len = count($array);
if($len <= 1)
{
return $array;
}
$key = $array[0];
$left = array();
$right = array();
for($i=1; $i<$len; ++$i)
{
if($array [$i] < $key)
{
$left[] = $array[$i];
}
else
$right[ ] = $array [$i];
}
}
$left = quickSort($left);
$right = quickSort($right);
return array_merge($left, array($key ), $right);
}

print '
';<p> print_r(quickSort(array(1,4,22,5,7,6,9)));<br> print '
';

4. Sorting effect
The process of sorting a column of numbers using quick sort

Sorting algorithm: PHP version quick sort, bubble sort_PHP tutorial

2. Bubble Sort
1. Introduction
Bubble Sort (Bubble Sort, translated in Taiwan as: bubble sort or bubble sort) is a simple sorting algorithm. It repeatedly walks through the sequence to be sorted, comparing two elements at a time and swapping them if they are in the wrong order. The work of visiting the array is repeated until no more exchanges are needed, which means that the array has been sorted. The name of this algorithm comes from the fact that smaller elements will slowly "float" to the top of the array through swapping.
2. Step
Compare adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element should be the largest number.
Repeat the above steps for all elements except the last one.
Continue repeating the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.
3. Code implementation

Copy code The code is as follows:
function bubbingSort(array $array)
{
for($i=0, $len=count($array)-1; $i<$len; ++ $i)
{
for($j=$len; $j>$i; --$j)
{
if($array[$j] < $array[$ j-1])
                                                                                                             $j- 1] = $temp;
}
}
}
return $array;
}

print '
';<br> print_r(bubbingSort(array(1,4,22,5,7,6,9))); print '
';


4. Sorting process

The process of sorting a column of numbers using bubble sort

Sorting algorithm: PHP version quick sort, bubble sort_PHP tutorial

http://www.bkjia.com/PHPjc/751510.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/751510.htmlTechArticle1. Quick sort 1. Introduction Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case scenario...
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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

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