Full implementation of PHP sorting algorithm _php tutorial

Source: Internet
Author: User
When learning PHP, you may encounter a PHP sorting problem, here will introduce a solution to the problem of PHP sorting, here to share with you. Every year always go out look at the data structure, each time always feel that many things did not learn, alas.

Today paste just use PHP implementation of 4 sorting algorithm, the other heap sort and merge sort is not written. Insert Sort, select sort, bubble sort, time complexity seems to be O (N2), so the actual meaning is not significant, in the actual test, I to 3,000 elements of the array, the three sorting algorithm takes 80 seconds or so, and the fast sort only takes 8 seconds, the gap is relatively large, interested can test their own. Let's take a closer look at the implementation of your PHP sorting algorithm.

 
 
  1. Insert sort (one-dimensional array)
  2. function Insert_sort ($arr) {
  3. $ Count Count = count ($arr);
  4. For ($i=1; $i<$count; $i + +) {
  5. $ tmp = $arr [$i];
  6. $ J = $i-1;
  7. While ($arr [$j] > $tmp) {
  8. $arr [$j +1] = $arr [$j];
  9. $arr [$j] = $tmp;
  10. $j--;
  11. }
  12. }
  13. return $arr;
  14. }
  15. Select sort (one-dimensional array)
  16. function Select_sort ($arr) {
  17. $ Count Count = count ($arr);
  18. For ($i=0; $i<$count; $i + +) {
  19. $ k = $i;
  20. For ($J= $i +1; $j<$count; $j + +) {
  21. if ($arr [$k] > $arr [$j])
  22. $ k = $j;
  23. if ($k! = $i) {
  24. $ tmp = $arr [$i];
  25. $arr [$i] = $arr [$k];
  26. $arr [$k] = $tmp;
  27. }
  28. }
  29. }
  30. return $arr;
  31. }
  32. Bubble sort (one-dimensional array)
  33. function Bubble_sort ($array) {
  34. $ Count Count = count ($array);
  35. if ($count <= 0) return false;
  36. For ($i=0; $i<$count; $i + +) {
  37. For ($J= $count-1; $j>$i; $j-) {
  38. if ($array [$j] < $array [$j-1]) {
  39. $ tmp = $array [$j];
  40. $array [$j] = $array [$j-1];
  41. $array [$j-1] = $tmp;
  42. }
  43. }
  44. }
  45. return $array;
  46. }
  47. Quick sort (one-dimensional array)
  48. function Quick_sort ($array) {
  49. if (count ($array) <= 1) return $array;
  50. $ Key = $array [0];
  51. $ Left_arr = Array ();
  52. $ Right_arr = Array ();
  53. For ($i=1; $i<count($array); $i + +) {
  54. if ($array [$i] <= $key)
  55. $left _arr[] = $array [$i];
  56. Else
  57. $right _arr[] = $array [$i];
  58. }
  59. $ Left_arr = Quick_sort ($left _arr);
  60. $ Right_arr = Quick_sort ($right _arr);
  61. Return Array_merge ($left _arr, Array ($key), $right _arr);
  62. }
  63. ?>

http://www.bkjia.com/PHPjc/446512.html www.bkjia.com true http://www.bkjia.com/PHPjc/446512.html techarticle When learning PHP, you may encounter a PHP sorting problem, here will introduce a solution to the problem of PHP sorting, here to share with you. Every year always go out to see the data ...

  • Related Article

    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.