Php bubble sorting algorithm implementation code

Source: Internet
Author: User
Php bubble sorting algorithm implementation code

  1. // Bubble sorting function
  2. // Reference this function to avoid memory consumption
  3. // Finishing: bbs.it-home.org
  4. Function & bubble (& $ arr ){
  5. $ Count = count ($ arr );
  6. If ($ count> 1 ){
  7. For ($ I = 0; $ I <$ count; $ I ++ ){
  8. For ($ j = $ count-1; $ j >=$ I; $ j --){
  9. If ($ arr [$ j-1]> $ arr [$ j]) {
  10. $ Temp = $ arr [$ j-1];
  11. $ Arr [$ j-1] = $ arr [$ j];
  12. $ Arr [$ j] = $ temp;
  13. }
  14. }
  15. }
  16. }
  17. Return $ arr;
  18. }
  19. // Output array (for viewing on the webpage)
  20. Function printArr (& $ arr ){
  21. Echo"
    ";  
  22. print_r($arr);
  23. echo "
  24. ";
  25. }
  26. // Start the test
  27. // Randomly assign ten values to the array
  28. For ($ I = 0; $ I <10; $ I ++ ){
  29. $ TestArr [] = rand (10,100 );
  30. }
  31. PrintArr (bubble ($ testArr ));
  32. ?>

>>> 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.