Home  >  Article  >  Backend Development  >  Simple bubbling method in PHP

Simple bubbling method in PHP

WBOY
WBOYOriginal
2016-07-25 09:02:25914browse
It's a very basic thing. I feel that the code is not concise enough. I hope experts can guide me to modify it.
Reprinted from PHP interview questions: http://phpmst.com/

  1. function BubbleSort($str){
  2. for($i=0;$i for ($ k=count($str)-2;$k>=$i;$k--){//Bubble this value forward;
  3. if($str[$k+1]<$str[$ k]){ //Change the less than sign to the greater than sign, which is to sort in descending order;
  4. $tmp=$str[$k+1];
  5. $str[$k+1]=$str[$k];
  6. $ str[$k]=$tmp;
  7. }
  8. }
  9. }
  10. return $str;
  11. }
  12. //The following is the test
  13. $str=array(5,8,2,6,10,0,3,12, 11);
  14. print_r(BubbleSort($str));
  15. ?>
Copy code


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