Home > Backend Development > PHP Tutorial > A simple example of implementing bubble sort in php

A simple example of implementing bubble sort in php

WBOY
Release: 2016-07-25 08:57:20
Original
1345 people have browsed it
  1. /**
  2. * php bubble sort
  3. * edit by bbs.it-home.org
  4. */
  5. function maopao($arr){
  6. $c = count($arr);
  7. $t = 0;
  8. for($i=0; $i<=$c;$i++){
  9. for($j=$i+1;$j<$c;$j++){
  10. if($arr[$i] < $arr[$j])
  11. {
  12. $t = $arr[$i];
  13. $arr[$i] = $arr[$j];
  14. $arr[$j] = $t;
  15. }
  16. }
  17. }
  18. return $arr;
  19. }
  20. //Call example
  21. $arr = array(9,3,12);
  22. print_r(maopao($arr));
  23. ?>
Copy code

More PHP implementations of bubble sorting , please refer to the following article: Small example of php bubble sort php bubble sort implementation code PHP array sorting method sharing (bubble sort, selection 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



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