PHP bubble sort algorithm implementation code

  • 2020-03-31 21:00:28
  • OfStack

The basic concept

The basic concept of bubble sort is to compare two adjacent Numbers in turn, putting the decimal in the front and the large number in the back. That is to compare the first and the second number, put the decimal before, after the large number. Then compare the second number with the third number, put the decimal before, put the large number after, and so on, until you compare the last two Numbers, put the decimal before, put the large number after. Repeat the above process, from the first log is still (because the number of exchange may be due to the number 2 and 3, stopped the number 1 is greater than the number 2), put before the decimal number after, has been compared to the minimum number of a pair of consecutive Numbers, put before the decimal number is put after the second trip ended, in the penultimate number to get a new minimum number. And so on until the sorting is finally done.
Since we always put the decimals in front of each other and the large Numbers in front of each other during the sorting process, this is the bubble sort.
The outer loop variable is set as I, and the inner loop variable is set as j. Repeat the outer loop 9 times, repeat the inner loop 9, 8,... 1 times. 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,... For each I, the value of j is 1,2... 10 - I.

produce
In many programs, we need to sort a sequence of Numbers to facilitate statistics. Common sorting methods are bubble sort, binary tree sort, selection sort, and so on. Bubble sort has always been popular because of its concise thinking method and relatively high efficiency.

Sorting process
Assumed that the array sorted R [1.. N] vertical erect, will have of the weight of each data element as a bubble, according to the principle of light bubbles cannot be under heavy bubble, scanning array R from down to up, every scan to the violation of the principle of light bubbles, will make it up "floating", so repeatedly, until the last any two bubbles is light person, the person that weigh in.

Update 2009-8-18: error updating code.


$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; 
} 
} 
} 

Using PHP for bubble sort, sort the array $a = array() from small to large


$a=array('3','8','1','4','11','7'); 
 print_r($a); 
 echo '<br/>'; 
 $len=count($a); 
 //Since the childhood
 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 simply implements bubble sort

When learning PHP, I dare not approach the algorithm. Even afraid of disturbing their own thoughts, now review is really that thing. Hey hey! Have you ever come across a situation like this?


<?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 bubble sort

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

The first:


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;
    }
  }
}

The second:


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;
    }
  }
}


Related articles: