Php selects the sorting algorithm implementation code

Source: Internet
Author: User
Php selects the sorting algorithm implementation code

  1. // Select the sorting function
  2. // Use references to reduce memory consumption
  3. // Finishing: bbs.it-home.org
  4. Function & select (& $ arr ){
  5. $ Count = count ($ arr );
  6. If ($ count> 1 ){
  7. For ($ I = 0; $ I <$ count-1; $ I ++ ){
  8. $ K = $ I;
  9. For ($ j = $ I + 1; $ j <$ count; $ j ++ ){
  10. If ($ arr [$ j] <$ arr [$ k]) {
  11. $ K = $ j;
  12. }
  13. }
  14. If ($ k! = $ I ){
  15. $ Tmp = $ arr [$ k];
  16. $ Arr [$ k] = $ arr [$ I];
  17. $ Arr [$ I] = $ tmp;
  18. }
  19. }
  20. }
  21. Return $ arr;
  22. }
  23. // Print the array function
  24. Function printArr (& $ arr ){
  25. Echo"
    ";  
  26. print_r($arr);
  27. echo "
  28. ";
  29. }
  30. // Start the test
  31. // Randomly assign ten values to the array
  32. For ($ I = 0; $ I <10; $ I ++ ){
  33. $ TestArr [] = rand (10,100 );
  34. }
  35. PrintArr (select ($ testArr ));
  36. ?>

>>> Articles you may be interested in: php examples of practical and fast sorting algorithms PHP implementation of various sorting algorithms small example of php bubble sorting php simple example of bubble sorting PHP implementation of two-dimensional array sorting user-defined function php selection of sorting implementation code php bubble sorting implementation code php insert sorting implementation code php quick sort) function php for fast sorting function php for bubble sorting exchange sorting method php for bubble sorting (bubble sort) php bubble sorting algorithm example php bubble sorting and quick sorting example php two-dimensional array sorting method php multi-dimensional array sorting

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.