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