Bubble Sorting of PHP Algorithms

Source: Internet
Author: User
When you have nothing to worry about, you will have a hard time thinking. I don't know what it is. that day, I suddenly thought of a university. When I first learned C. the instructor sorted the bubbles. then I began to think about using PHP to implement a note. * ** bubble sort (one-dimensional array) * compares the sizes of elements in the data to be sorted. If the order of the two data elements is the opposite, the data is exchanged directly.

When you have nothing to worry about, you will have a hard time thinking. I don't know what it is. that day, I suddenly thought of a university. When I first learned C. the instructor sorted the bubbles. then I began to think about using PHP to implement a note. /*** bubble sort (one-dimensional array) * compares the sizes of the elements of the data to be sorted. If the order of the two data elements is the opposite, the data is exchanged directly.

When you have nothing to worry about, you have to think about it.

I suddenly thought of College on this day. When I first learned C, my teacher talked about the bubble sort. then I began to think about using PHP to make a note.

/*** Bubble sort (one-dimensional array) * compares the sizes of elements in the data to be sorted. If the order of the two data elements is the opposite, until there is no data element in reverse order * Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, scanning the array from bottom up, where a light bubble that violates the principle is scanned, it will "float" up ". this is repeated. * until the last two parties are put on the lighter ones, the heavier ones will be placed on the lighter ones. */function bubble_sort ($ arr) {$ count = count ($ arr); if ($ count = 0 | is_array ($ arr) {echo 'not NULL array '; return false;} for ($ I = 0; $ I <$ count; $ I ++) {$ flag = 1; for ($ j = $ count-1; $ j> $ I; $ j --) {if ($ arr [$ j] <$ arr [$ j-1]) {$ tmp = $ arr [$ j]; // Replace the array elements with $ arr [$ j] = $ array [$ j-1]; $ arr [$ j-1] = $ tmp; $ flag = 0;} if (1 = $ flag) {echo $ I, 'sorted'; break ;}} return $ arr ;}

Original article address: Bubble sorting of the PHP algorithm. Thank you for sharing it with the original author.

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.