Implementation code of PHP bubble sort algorithm

basic concept

The basic concept of bubble sort is to compare two adjacent numbers in turn, placing the small number in the front and the large number in the back. That is, first compare the 1st and 2nd numbers, put the decimal first and the large number after. Then compare the second number and the third number, put the decimal before the large number, and continue in this way until the last two numbers are compared, putting the decimal before the large number and after the large number. Repeat the above process, still start the comparison from the first logarithm (because the first number may no longer be greater than the second number due to the exchange of the second number and the third number), put the decimal before the large number Then, compare the pair of adjacent numbers before the minimum number, put the decimal number first, and put the larger number after the second pass ends, and get a new minimum number in the penultimate number. And so on until the sorting is finally completed. 
Because in the sorting process, the decimals are always placed forward and the large numbers are placed backward, which is equivalent to the upward bubbles, so it is called bubble sorting. 
It is implemented with a double loop, 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 in turn. The two elements that are compared each time are related to the inner loop j, they can be identified by a[j] and a[j+1] respectively, the value of i is 1,2,...,9 in order, for each The values ​​of i, j are 1,2,...10-i in order. In many programming, we need to sort an array to facilitate statistics. Common sorting methods include bubble sort, binary tree sort, selection sort, and so on


The bubble sort has always been favored because of its concise thinking method and relatively high efficiency. 

The sorting process 
assumes that the sorted array R[1..N] is vertically erected, and each data element is regarded as a bubble with weight. According to the principle that light bubbles cannot be under heavy bubbles, the array R is scanned from bottom to top. Scan a light bubble that violates this principle, and make it "float" upward, and so on, until finally any two bubbles are the lighter one above and the heavier one below. 

Update 2009-8-18: Update code bug.

$arr = array(345,4,17,6,52,16,58,69,32,8,234);
for($i=1;$i<count($arr);$i++){
for($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;
}
}
}

Please use PHP to implement bubble sort, sort the array $a = array() in ascending order

$a=array('3','8','1','4','11','7');
 print_r($a);
 echo '<br/>';
 $len=count($a);
 //From small to large
 for($i=1;$i<$len;$i++){
 for($j=$len-1;$j>=$i;$j--){
 if($a[$j]<$a[$j-1]){
 $x=$a[$j];
  $a[$j]=$a[$j-1];
 $a[$j-1]=$x;
 }
}
}

PHP simple implementation of bubble sort

When learning PHP, I dare not approach the algorithm. I'm just afraid of disturbing my thoughts, but looking back now, it's really the same thing. Hey-hey! Have you encountered such a situation?

<?php
#bubble sort
$arr = array(12,45,89,3,24,55,223,76,22,11,89,2,4,5,28,112,20,434,23,65,65,765,6,8,23,5,33,553,45,423,64,77,84,23);
$tmp;
for($i=0;$i<count($arr)-1;$i++ ){    
  for($j=0;$j<count($arr)-1-$i;$j++){
    if($arr[$j] > $arr[$j+1]){
      $tmp = $arr[$j];
      $arr[$j] = $arr[$j+1];
      $arr[$j+1] = $tmp;
    } 
  }
} 
print_r($arr);

php冒泡排序

$b=array('4','3','8','9','2','1');
$len=count($b);//6

第一种:

for($k=0;$k<=$len;$k++)
{
  for($j=$len-1;$j>$k;$j--){
    if($b[$j]<$b[$j-1]){
      $temp = $b[$j];
      $b[$j] = $b[$j-1];
      $b[$j-1] = $temp;
    }
  }
}

第二种:

for($k=1;$k<$len;$k++)
{
  for($j=0;$j<$len-$k;$j++){
    if($b[$j]>$b[$j+1]){
      $temp =$b[$j+1];
      $b[$j+1] =$b[$j] ;
      $b[$j] = $temp;
    }
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809909&siteId=291194637