Home  >  Article  >  Backend Development  >  Summary of implementation of various sorting algorithms in PHP

Summary of implementation of various sorting algorithms in PHP

WBOY
WBOYOriginal
2016-07-25 08:55:46878browse
  1. // Bubble sort

  2. function BubbleSort($arr) {
  3. // Get the total length of the array
  4. $num = count($arr);
  5. // Forward Traverse the array
  6. for ($i = 1; $i < $num; $i++) {
  7. // Reverse traverse
  8. for ($j = $num - 1; $j >= $i ; $j-- ) {
  9. // Compare two adjacent numbers
  10. if ($arr[$j] < $arr[$j-1]) {
  11. // Temporarily store the smaller number
  12. $iTemp = $arr[$j -1];
  13. // Put the bigger ones in front
  14. $arr[$j-1] = $arr[$j];
  15. // Put the smaller ones in the back
  16. $arr[$j] = $iTemp;
  17. }
  18. }
  19. }
  20. return $arr;
  21. }

  22. // Exchange sorting

  23. function ExchangeSort($arr){
  24. $num = count($arr);
  25. // Traverse the array
  26. for ($i = 0;$i < $num - 1; $i++) {
  27. // Get the next index of the current index
  28. for ($j = $i + 1; $j < $num; $ j++) {
  29. // Compare two adjacent values ​​​​
  30. if ($arr[$j] < $arr[$i]) {
  31. // Temporarily store the smaller number
  32. $iTemp = $arr[$ i];
  33. // Put the bigger ones in front
  34. $arr[$i] = $arr[$j];
  35. // Put the smaller ones in the back
  36. $arr[$j] = $iTemp;
  37. }
  38. }
  39. } // bbs.it-home.org
  40. return $arr;
  41. }

  42. // Selection sorting

  43. function SelectSort($arr) {
  44. // Get the total length of the array
  45. $ num = count($arr);
  46. // Traverse the array
  47. for ($i = 0;$i < $num-1; $i++) {
  48. // Temporarily store the current value
  49. $iTemp = $arr[$i ];
  50. // Temporarily store the current position
  51. $iPos = $i;
  52. // Traverse the data after the current position
  53. for ($j = $i + 1;$j < $num; $j++){
  54. // If there is a value smaller than the current value
  55. if ($arr[$j] < $iTemp) {
  56. // Temporary minimum value
  57. $iTemp = $arr[$j];
  58. // Temporary position
  59. $iPos = $ j;
  60. }
  61. }
  62. // Put the current value into the calculated position
  63. $arr[$iPos] = $arr[$i];
  64. // Change the current value into the calculated value
  65. $arr[$ i] = $iTemp;
  66. }
  67. return $arr;
  68. }

  69. // Insertion sorting

  70. function InsertSort($arr){
  71. $num = count($arr);
  72. / / Traverse the array
  73. for ($i = 1;$i < $num; $i++) {
  74. // Get the current value
  75. $iTemp = $arr[$i];
  76. // Get the previous position of the current value
  77. $iPos = $i - 1;
  78. // If the current value is less than the previous value, it has not reached the beginning of the array
  79. while (($iPos >= 0) && ($iTemp < $arr[$iPos])) {
  80. // Put the previous value one digit back
  81. $arr[$iPos + 1] = $arr[$iPos];
  82. // Decrement the position
  83. $iPos--;
  84. }
  85. $arr[$iPos+ 1] = $iTemp;
  86. }
  87. return $arr;
  88. }

  89. // Quick sort

  90. function QuickSort($arr){
  91. $num = count($arr);
  92. $l = $r = 0;
  93. // Traverse the array starting from the second index
  94. for ($i = 1;$i < $num; $i++) {
  95. // If the value is less than index 1
  96. if ($arr [$i] < $arr[0]) {
  97. // Load the left index array (data less than index 1)
  98. $left[] = $arr[$i];
  99. $l++;
  100. } else { / /bbs.it-home.org
  101. // Otherwise, load into the right index (data greater than index 1)
  102. $right[] = $arr[$i];
  103. $r++; //
  104. }
  105. }
  106. // If the left index has a value, sort the left index
  107. if($l > 1) {
  108. $left = QuickSort($left);
  109. }
  110. // Sorted array
  111. $new_arr = $left;
  112. // Will The current array is put first at the end
  113. $new_arr[] = $arr[0];
  114. // If the index has a value, sort the right index
  115. if ($r > 1) {
  116. $right = QuickSort($ right);
  117. }
  118. // Add data again according to the length of the right index
  119. for($i = 0;$i < $r; $i++) {
  120. $new_arr[] = $right[$i];
  121. }
  122. return $new_arr;
  123. }
  124. ?>

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