


Sorting algorithm: PHP version quick sort, bubble sort_PHP tutorial
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
{
$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 '';
The process of sorting a column of numbers using quick sort
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
{
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

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.
