Home  >  Article  >  Backend Development  >  PHP array sorting method sharing (bubble sort, selection sort)

PHP array sorting method sharing (bubble sort, selection sort)

WBOY
WBOYOriginal
2016-07-25 09:04:041186browse
  1. function maoPao($arr,$style)//By default, the value is passed, not the address. If you add an & before $arr, it points to the same address as $arr1, and $arr1 outside the function is also arranged
  2. {
  3. $temp=0;
  4. $flag=false;
  5. for($i=0; $i{
  6. for($j=0;$j{
  7. if($style== 'bts') $op=$arr[$j]<$arr[$j+1];
  8. else if($style=='stb') $op=$arr[$j]>$arr[ $j+1];
  9. if($op)
  10. {
  11. $temp=$arr[$j];
  12. $arr[$j]=$arr[$j+1];
  13. $arr[$j+1 ]=$temp;
  14. $flag=true;
  15. }
  16. }
  17. if($flag==false)
  18. {
  19. break;//When a horizontal loop comes down, flag==false; indicates that each adjacent element in the vertical loop When comparing sizes, the if conditions are not met, that is, they have been arranged from small to large, and there is no need to loop horizontally
  20. }
  21. }
  22. foreach ($arr as $key => $value)
  23. {
  24. echo $value.',';
  25. }
  26. }
  27. $arr1=array(101,101,-9,-8,0,76,1,57,43,90,23,-56);
  28. maoPao($arr1,'stb');//small to big
  29. ?>
Copy code

For examples of bubble sorting, you can also refer to the following articles: php bubble sort exchange sort method Another 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 2. Select sorting: The second to nth numbers are compared with the first number respectively and exchanged, and the third to nth numbers are compared with the second number respectively and exchanged until the sorting is completed.

  1. /**

  2. *
  3. *
  4. *
  5. */
  6. function selectSort($arr,$style)
  7. {
  8. $temp=0;
  9. $flag=false;
  10. for ($i=0;$i{
  11. for($j=$i+1;$j{
  12. if( $style=='bts') $op=$arr[$i]<$arr[$j];
  13. else if($style=='stb') $op=$arr[$i]>$ arr[$j];
  14. if($op)
  15. {
  16. $temp=$arr[$i];
  17. $arr[$i]=$arr[$j];
  18. $arr[$j]=$temp ;
  19. $flag=true;
  20. }
  21. }
  22. if($flag==false)
  23. {
  24. break;
  25. }
  26. }
  27. foreach ($arr as $key => $value)
  28. {
  29. echo $value. ',';
  30. }
  31. }
  32. $arr1=array(21.5,33,90,7,-4,5,55,11);
  33. selectSort($arr1,'stb');

  34. < ;p>function selectSort($arr,$style)
  35. {
  36. $temp=0;
  37. $flag=false;
  38. for($i=0;$i{
  39. for($j=$i+1;$j{
  40. if($style=='bts') $op=$arr[$i]<$arr[ $j];
  41. else if($style=='stb') $op=$arr[$i]>$arr[$j];
  42. if($op)
  43. {
  44. $temp=$arr[$ i];
  45. $arr[$i]=$arr[$j];
  46. $arr[$j]=$temp;
  47. $flag=true;
  48. }
  49. }
  50. if($flag==false)
  51. {
  52. break;
  53. }
  54. }
  55. foreach ($arr as $key => $value)
  56. {
  57. echo $value.',';
  58. }
  59. }
  60. $arr1=array(21.5,33,90,7, -4,5,55,11);
  61. selectSort($arr1,'stb');
  62. echo "
    ";
  63. ?>

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