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.