Home >Backend Development >PHP Tutorial >Implementation code of PHP bubble sort algorithm_PHP tutorial

Implementation code of PHP bubble sort algorithm_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:37989browse

Copy code The code is as follows:

$arr = array(345,4,17,6,52,16,58,69, 32,8,234);
for($i=1;$ifor($j=count($arr)-1;$j>=$i ;$j--){
if($arr[$j]<$arr[$j-1]){
$temp = $arr[$j-1];
$arr [$j-1] = $arr[$j];
$arr[$j] = $temp;
}
}
}

Basic concept
The basic concept of bubble sorting is: compare two adjacent numbers in sequence, put the decimal in the front and the large number in the back. That is, first compare the first and second numbers, put the decimals first and the large numbers last. Then compare the second number and the third number, put the decimal in front and the large number in the back, and continue like this until comparing the last two numbers, put the decimal in front and the large number in the back. Repeat the above process, still starting from the first pair of numbers (because it may be due to the exchange of the second number and the third number that the first number is no longer greater than the second number), put the decimal first and the large number first. Finally, compare until a pair of adjacent numbers before the smallest number, put the decimal in front and the large number in the back. The second pass ends and a new minimum number is obtained in the penultimate number. Continue like this until the sorting is finally completed.
Since in the sorting process, decimals are always placed forward and large numbers are placed backward, which is equivalent to bubbles rising, so it is called bubble sorting.
Use a double loop to implement, the outer loop variable is set to i, and the inner loop variable is set to j. The outer loop is repeated 9 times, and the inner loop is repeated 9, 8,..., 1 time. The two elements compared each time are related to the inner loop j. They can be identified by a[j] and a[j+1] respectively. The values ​​​​of i are 1, 2,...,9 in order. For each The values ​​of i and j are 1,2,…10-i in sequence.

produces
In many programming, we need to sort a sequence to facilitate statistics. Common sorting methods include bubble sort, binary tree sort, selection sort, etc. . Bubble sorting has always been popular due to its concise thinking method and relatively high efficiency.

The sorting process
Imagine the sorted array R[1..N] to be erected vertically, and each data element is regarded as a bubble with weight. According to the light bubble, it cannot be placed in a heavy bubble. The principle under the bubbles is to scan the array R from bottom to top. If any light bubbles that violate this principle are scanned, they will be "floated" upward. This is repeated until the last two bubbles are the lighter one on top and the heavier one on top. Until the next one.

Update 2009-8-18: Update code error.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322346.htmlTechArticleCopy the code The code is as follows: $arr = array(345,4,17,6,52,16,58, 69,32,8,234); for($i=1;$icount($arr);$i++){ for($j=count($arr)-1;$j=$i;$j--){ if($arr[$j]$arr[$j-1]){ $temp = $arr[$j-1]...
Statement:
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