Heim  >  Artikel  >  Backend-Entwicklung  >  排序

排序

WBOY
WBOYOriginal
2016-07-25 08:49:54786Durchsuche
  1. // 选择排序 不稳定排序
  2. function selection_sort($array)
  3. {
  4. $max = count($array) - 1;
  5. for($i = 0; $i {
  6. $min = $i;
  7. for($j = $i + 1; $j {
  8. if($array[$j] {
  9. $min = $j;
  10. }
  11. }
  12. if($min != $i)
  13. {
  14. $temp = $array[$min];
  15. $array[$min] = $array[$i];
  16. $array[$i] = $temp;
  17. }
  18. }
  19. return $array;
  20. }
  21. // foreach while 插入排序
  22. function insertsort($arr)
  23. {
  24. foreach($arr as $k => $v)
  25. {
  26. $i = $k - 1;
  27. while($i > -1 && $v {
  28. $next = $arr[$i + 1];
  29. $arr[$i + 1] = $arr[$i];
  30. $arr[$i] = $next;
  31. $i--;
  32. }
  33. }
  34. return $arr;
  35. }
  36. // for while 插入排序
  37. function insertsort1($arr)
  38. {
  39. $max_key = count($arr) - 1;
  40. for($i = 1; $i {
  41. $j = $i - 1;
  42. $current = $arr[$i];
  43. while($j >= 0 && $arr[$j] > $current)
  44. {
  45. $temp = $arr[$j+1];
  46. $arr[$j+1] = $arr[$j];
  47. $arr[$j] = $temp;
  48. $j--;
  49. }
  50. }
  51. return $arr;
  52. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:检测文件是否有bom头 - PHP Nächster Artikel:多文件上传 - PHP