I feel that the code is not concise enough. I hope that the experts can guide the modification. I have prepared a few more articles for you to facilitate testing and learning basic things. I feel that the code is not concise enough, hope experts can guide the modification
The code is as follows:
Function BubbleSort ($ str ){
For ($ I = 0; $ I For ($ k = count ($ str)-2; $ k >=$ I; $ k --) {// bubble this value forward;
If ($ str [$ k + 1] <$ str [$ k]) {// change the minor signs to greater than signs, that is, sort them in descending order;
$ Tmp = $ str [$ k + 1];
$ Str [$ k + 1] = $ str [$ k];
$ Str [$ k] = $ tmp;
}
}
}
Return $ str;
}
// The following is the test
$ Str = array (, 0, 11 );
Print_r (BubbleSort ($ str ));
?>
Php bubble sorting 2
The basic concept is: compare the adjacent two numbers in sequence, put the decimal points in front, and put the large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. Repeat the above process and compare it from the first logarithm (because of the exchange of 2nd numbers and 3rd numbers, the number of 1st numbers is no longer less than 2nd, always compare to the adjacent number before the maximum number. Place the decimal number before, place the large number, and end the second row. in the second to last number, a new maximum number is obtained. So on until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
It is implemented using a double loop. the external loop variable is set to I and the inner loop variable is set to j. The external loop repeats 9 times, and the inner loop repeats 9, 8,..., 1 time in sequence. The two elements for each comparison are related to the inner loop j, which can be identified by a [j] and a [j + 1] respectively, the values of I are 1, 2 ,..., 9. for each I, j's
The values are 1, 2,... 10-i in sequence.
The code is as follows:
Function asc ($)
{
For ($ I = 0; $ I {
For ($ j = 0; $ j {
If ($ a [$ j]> $ a [$ j + 1])
{
$ Tmp = $ a [$ j + 1];
$ A [$ j + 1] = $ a [$ j];
$ A [$ j] = $ tmp;
}
}
}
Print_r ($ );
}
$ A = array (9,8, 17,6, 26,4, 33,2, 1 );
Print_r (asc ($ ));
?>
Function desc ($)
{
$ C = array ();
For ($ I = count ($ a)-1; $ I> 0; $ I --)
{
For ($ j = 0; $ j {
If ($ a [$ j] <$ a [$ j + 1])
{
$ Tmp = $ a [$ j + 1];
$ A [$ j + 1] = $ a [$ j];
$ A [$ j] = $ tmp;
}
}
}
Print_r ($ );
}
$ Arr = array (, 59 );
Desc ($ arr );
?>
PHP bubble sorting demonstration
In the previous interview, the Examiner wrote a pen question, and thought XX was the case. the program should be written on the computer, not on the pen.
PHP program file sort_bubble_up.php
The code is as follows:
Demonstration of bubble sorting
Demonstration of bubble sorting
// Randomly generate an array$ Arr = array ();Echo'
Initial value |
';Echo'
';For ($ I = 0; $ I <10; $ I ++ ){$ Arr [$ I] = rand ();Echo"
\ $ Arr [$ I] ={$ arr [$ I]} | ";}// Sort by bubbleFor ($ I = 9; $ I> 0; $ I --){Echo'
Times '. (10-$ I ).' |
';For ($ j = 0; $ j <$ I; $ j ++ ){If ($ arr [$ j] <$ arr [$ j + 1]) {$ Tmp = $ arr [$ j];$ Arr [$ j] = $ arr [$ j + 1];$ Arr [$ j + 1] = $ tmp;}Echo'
';For ($ k = 0; $ k <10; $ k ++ ){Switch ($ k ){Case $ j: echo'
'; Break; Case $ j + 1: echo' |
'; Break; Default: echo' |
'; } Echo "\ $ arr [$ k] ={$ arr [$ k]} | ";}Echo'
';}}// Display the sorting resultEcho'
Result |
';Echo'
';For ($ I = 0; $ I <10; $ I ++ ){Echo"
\ $ Arr [$ I] ={$ arr [$ I]} | ";}Echo'
';?>
Sample table file sort.css
The code is as follows:
H1 {text-align: center; color: blue ;}
Table {font-size: 12px; font-family: arial; background-color: black; text-align: center ;}
Td {background-color: white ;}
. Base {background-color: # 0FF ;}
. Light {background-color: # 0DD ;}
. Title {background-color: # 3FF; text-align: center ;}