PHP常用排序算法实例小结【基本排序,冒泡排序,快速排序

  1. classbevin{
  2. public$public='public'
  3. private$private='private'
  4. protected$protected='protected'
  5. //final $final = 'final'
  6. static$static='static'
  7. function__construct(){
  8. $this->protected='change private'
  9. }
  10. publicfunctionsetValue($a){
  11. self::$static=$a;
  12. }
  13. publicfunctiongetValue(){
  14. echo$this->private;
  15. }
  16. function__destruct(){
  17. echo'asdfsadf'
  18. }
  19. }
  20. classpaixu {
  21. // 基本排序
  22. publicfunctiont_sortArray($array) {
  23. if(is_array($array) &&count($array)>1) {
  24. for($i=0;$i<count($array);$i++) {
  25. for($j=($i+1);$j<count($array);$j++) {
  26. $temp=$array[$i];
  27. if($array[$j]<$array[$i]) {
  28. $array[$i] =$array[$j];
  29. $array[$j] =$temp;
  30. }
  31. }
  32. }
  33. return$array;
  34. }else{
  35. return$array;
  36. }
  37. }
  38. // 冒泡排序
  39. publicfunctionc_sortArray($array) {
  40. if(!is_array($array) ||count($array)<=1){return$array; }
  41. $status= false;
  42. <a href="/tags.php/foreach/" target="_blank">foreach</a>($arrayas$key=>$v) {
  43. if($key>0) {
  44. if($array[$key-1]>$array[$key]) {
  45. $array[$key] =$array[$key-1];
  46. $array[$key-1] =$v;
  47. $status= true;
  48. }
  49. }
  50. }
  51. if($status) {
  52. return$this->c_sortArray($array);
  53. }else{
  54. return$array;
  55. }
  56. }
  57. // 快速排序
  58. publicfunctionv_sortArray($array) {
  59. if(!is_array($array) ||count($array)<=1){return$array; }
  60. if(count($array)>2) {
  61. $m=$array[floor(count($array)/2)+1];
  62. }else{
  63. if($array[0]>$array[1]) {
  64. $temp=$array[0];
  65. $array[0] =$array[1];
  66. $array[1] =$temp;
  67. }
  68. return$array;
  69. }
  70. $leftarray=array();
  71. $rightarray=array();
  72. foreach($arrayas$key=>$v) {
  73. if($v>$m) {
  74. $rightarray[] =$v;
  75. }
  76. if($v<$m) {
  77. $leftarray[] =$v;
  78. }
  79. if($v==$m) {
  80. $mid[] =$v;
  81. }
  82. }
  83. $nleftarray=$this->v_sortArray($leftarray);
  84. $nrightarray=$this->v_sortArray($rightarray);
  85. returnarray_merge($nleftarray,$mid,$nrightarray);
  86. }
  87. // 直接插入排序
  88. publicfunctioni_sortArray($array) {
  89. if(!is_array($array) ||count($array)<=1){return$array; }
  90. $newarray=array($array[0]);
  91. $temp= 0;
  92. foreach($arrayas$k=>$v) {
  93. if($k>0) {
  94. if($v>=$newarray[count($newarray)-1]) {
  95. $newarray[] =$v;
  96. }else{
  97. foreach($newarrayas$nk=>$nv) {
  98. if($v<$nv) {
  99. $temparray=array();
  100. foreach($newarrayas$ck=>$cv) {
  101. if($ck<$nk) {
  102. $temparray[$ck] =$cv;
  103. }elseif($ck==$nk) {
  104. $temparray[$ck] =$v;
  105. $temparray[($ck+1)] =$cv;
  106. }else{
  107. $temparray[($ck+1)] =$cv;
  108. }
  109. }
  110. $newarray=$temparray;
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return$newarray;
  118. }
  119. }
  120. $bevin=newpaixu;
  121. $array=array(5,4,5,4,4,5,5,5,5,5);
  122. $v=$bevin->t_sortArray($array);
  123. print_r($v);
  124. $v=$bevin->c_sortArray($array);
  125. print_r($v);
  126. $v=$bevin->v_sortArray($array);
  127. print_r($v);
  128. $v=$bevin->i_sortArray($array);
  129. print_r($v);

运行结果:

  1. Array
  2. (
  3. [0] => 4
  4. [1] => 4
  5. [2] => 4
  6. [3] => 5
  7. [4] => 5
  8. [5] => 5
  9. [6] => 5
  10. [7] => 5
  11. [8] => 5
  12. [9] => 5
  13. )
  14. Array
  15. (
  16. [0] => 4
  17. [1] => 4
  18. [2] => 4
  19. [3] => 5
  20. [4] => 5
  21. [5] => 5
  22. [6] => 5
  23. [7] => 5
  24. [8] => 5
  25. [9] => 5
  26. )
  27. Array
  28. (
  29. [0] => 4
  30. [1] => 4
  31. [2] => 4
  32. [3] => 5
  33. [4] => 5
  34. [5] => 5
  35. [6] => 5
  36. [7] => 5
  37. [8] => 5
  38. [9] => 5
  39. )
  40. Array
  41. (
  42. [0] => 4
  43. [1] => 4
  44. [2] => 4
  45. [3] => 5
  46. [4] => 5
  47. [5] => 5
  48. [6] => 5
  49. [7] => 5
  50. [8] => 5
  51. [9] => 5
  52. )