Php array sorting method sharing (bubble sorting, select sorting)

Source: Internet
Author: User
Php array sorting method sharing (bubble sorting, select sorting)

  1. Function maoPao ($ arr, $ style) // The default value is passed, not the address. If you add an ampersands (&) before $ arr, it points to the same address as $ arr1, and $ arr1 outside the function is also scheduled.
  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 = 'stab') $ 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; // flag = false when a horizontal loop is executed; this indicates that if conditions are not met for each neighboring element in a vertical loop when the values are larger, that is, the conditions are arranged from small to large, horizontal Loop not required
  22. }
  23. }
  24. Foreach ($ arr as $ key => $ value)
  25. {
  26. Echo $ value .',';
  27. }
  28. }
  29. $ Arr1 = array (101,101,-9,-,-56 );
  30. MaoPao ($ arr1, 'stab'); // small to big
  31. ?>

For examples of bubble sorting, you can refer to the following article: Another php bubble sorting (bubble sort) example php code for implementing the bubble sorting algorithm php bubble sorting algorithm example php bubble sorting and quick sorting example 2. select sorting: The second number to the nth number are compared with the first number, respectively, exchange, and compare the numbers from the third to the nth to the second 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 = 'stab') $ 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. $ Arr1 = array (21.5, 7 );
  35. SelectSort ($ arr1, 'stab ');

  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 = 'stab') $ 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. $ Arr1 = array (21.5, 7 );
  65. SelectSort ($ arr1, 'stab ');
  66. Echo"
    ";
  67. ?>

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.