PHP array sorting method Sharing (bubble sort, select sort)

Source: Internet
Author: User
  1. function Maopao ($arr, $style)//The value is passed by default, not the address. If you add a & before $arr, and $arr1 point to the same address, the $ARR1 outside the function is also lined up.
  2. {
  3. $temp = 0;
  4. $flag =false;
  5. for ($i =0; $i
  6. {
  7. for ($j =0; $j
  8. {
  9. if ($style = = ' BTS ') $op = $arr [$j]< $arr [$j +1];
  10. else if ($style = = ' STB ') $op = $arr [$j]> $arr [$j +1];
  11. if ($OP)
  12. {
  13. $temp = $arr [$j];
  14. $arr [$j]= $arr [$j +1];
  15. $arr [$j +1]= $temp;
  16. $flag =true;
  17. }
  18. }
  19. if ($flag ==false)
  20. {
  21. break;//when a transverse cycle down the flag==false, indicating that each of the adjacent elements in the longitudinal cycle is relatively large hours if the condition is not satisfied, that is, from small to large has been lined up, no further transverse cycle
  22. }
  23. }
  24. foreach ($arr as $key = $value)
  25. {
  26. echo $value. ', ';
  27. }
  28. }
  29. $arr 1=array (101,101,-9,-8,0,76,1,57,43,90,23,-56);
  30. Maopao ($arr 1, ' STB ');//small to Big
  31. ?>
Copy Code

For examples of bubble sorting, you can also refer to the following article: Php bubble sort of exchange sorting method another PHP bubble sort (bubble sort) example PHP implementation bubble sort algorithm code PHP bubble sorting algorithm A case of PHP bubble sort and Quick sort Example 2, Select Sort: The second number to the nth number is compared with the first number, and the third number to the nth number is compared with the second number respectively, and is exchanged until the sorting is complete.

  1. /**

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

  36. function Selectsort ($arr, $style)

  37. {
  38. $temp = 0;
  39. $flag =false;
  40. for ($i =0; $i
  41. {
  42. for ($j = $i +1; $j
  43. {
  44. if ($style = = ' BTS ') $op = $arr [$i]< $arr [$j];
  45. else if ($style = = ' STB ') $op = $arr [$i]> $arr [$j];
  46. if ($OP)
  47. {
  48. $temp = $arr [$i];
  49. $arr [$i]= $arr [$j];
  50. $arr [$j]= $temp;
  51. $flag =true;
  52. }
  53. }
  54. if ($flag ==false)
  55. {
  56. Break
  57. }
  58. }
  59. foreach ($arr as $key = $value)
  60. {
  61. echo $value. ', ';
  62. }
  63. }
  64. $arr 1=array (21.5,33,90,7,-4,5,55,11);
  65. Selectsort ($arr 1, ' STB ');
  66. echo "
    ";
  67. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.