Home > Backend Development > PHP Tutorial > PHP bubble sort algorithm implementation code

PHP bubble sort algorithm implementation code

WBOY
Release: 2016-07-25 08:54:17
Original
1069 people have browsed it
  1. //Bubble sort function
  2. //This function uses references to avoid memory consumption
  3. //Organization: bbs.it-home.org
  4. function &bubble(&$arr){
  5. $ count=count($arr);
  6. if($count>1){
  7. for($i=0;$i<$count;$i++){
  8. for($j=$count-1;$j>= $i;$j--){
  9. if($arr[$j-1]>$arr[$j]){
  10. $temp=$arr[$j-1];
  11. $arr[$j- 1]=$arr[$j];
  12. $arr[$j]=$temp;
  13. }
  14. }
  15. }
  16. }
  17. return $arr;
  18. }
  19. //Output array (convenient for viewing on the web page)
  20. function printArr(&$arr){
  21. echo "
    "; </li>
    <li> print_r($arr); </li>
    <li> echo "
    ";
  22. }
  23. //Start the test
  24. //Assign ten randomly to the array Value
  25. for($i=0;$i<10;$i++){
  26. $testArr[]=rand(10,100);
  27. }
  28. printArr(bubble($testArr));
  29. ?>
Copy Code

>>>> Articles you may be interested in: PHP practical quick sorting algorithm example code A summary of the implementation of various sorting algorithms in PHP Small example of php bubble sort A simple example of implementing bubble sort in php PHP two-dimensional array sorting custom function php selection sort implementation code php bubble sort implementation code php insertion sort implementation code php function to implement quick sort php function to implement quick sort php bubble sort exchange sort method Example of php bubble sort php code to implement bubble sort algorithm An example of php bubble sorting algorithm Examples of php bubble sort and quick sort Two methods of php two-dimensional array sorting php multidimensional array sorting



source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template