PHP sorting algorithm-PHP Tutorial

Source: Internet
Author: User
PHP sorting algorithm is fully implemented. When learning PHP, you may encounter a PHP sorting problem. here we will introduce the solution to the PHP sorting problem, and share it with you here. Every year, you may encounter a PHP sorting problem when reading data to learn PHP. here we will introduce the solution to the PHP sorting problem, and share it with you. Every year, we always have to look at the data structure every other time. every time I feel that I have not learned many things well, alas.

Today, I used php to implement the 4 sorting algorithm. In addition, the heap sorting and merge sorting are not written. Insert sorting, select sorting, and bubble sorting. The time complexity seems to be O (N2), so it is of little practical significance. in actual testing, I performed 3000 array elements, it takes about 80 seconds for these three sorting algorithms, and 8 seconds for quick sorting. the gap is indeed large. if you are interested, you can test it on your own. Let's take a detailed look at the implementation of your PHP sorting algorithm.

 
  1. // Insert sorting (one-dimensional array)
  2. Function insert_sort ($ arr ){
  3. $CountCount= 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. $CountCount= 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. $CountCount= 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. // Fast sorting (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. ?>

For the sort PHP sorting problem, here we will introduce the solution to the PHP sorting problem. here we will share it with you. Every year, we always have to look at the data every three to five minutes...

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.