- Function maoPao ($ arr, $ style) // The default value is passed, not the address. If you add an ampersands (&) before $ arr, it points to the same address as $ arr1, and $ arr1 outside the function is also scheduled.
- {
- $ Temp = 0;
- $ Flag = false;
- For ($ I = 0; $ I
- {
- For ($ j = 0; $ j
- {
- If ($ style = 'bts') $ op = $ arr [$ j] <$ arr [$ j + 1];
- Else if ($ style = 'stab') $ op = $ arr [$ j]> $ arr [$ j + 1];
- If ($ op)
- {
- $ Temp = $ arr [$ j];
- $ Arr [$ j] = $ arr [$ j + 1];
- $ Arr [$ j + 1] = $ temp;
- $ Flag = true;
- }
- }
- If ($ flag = false)
- {
- Break; // flag = false when a horizontal loop is executed; this indicates that if conditions are not met for each neighboring element in a vertical loop when the values are larger, that is, the conditions are arranged from small to large, horizontal Loop not required
- }
- }
- Foreach ($ arr as $ key => $ value)
- {
- Echo $ value .',';
- }
- }
- $ Arr1 = array (101,101,-9,-,-56 );
- MaoPao ($ arr1, 'stab'); // small to big
- ?>
For examples of bubble sorting, you can refer to the following article: Another php bubble sorting (bubble sort) example php code for implementing the bubble sorting algorithm php bubble sorting algorithm example php bubble sorting and quick sorting example 2. select sorting: The second number to the nth number are compared with the first number, respectively, exchange, and compare the numbers from the third to the nth to the second until the sorting is complete.
-
/**
- *
- *
- *
- */
- Function selectSort ($ arr, $ style)
- {
- $ Temp = 0;
- $ Flag = false;
- For ($ I = 0; $ I
- {
- For ($ j = $ I + 1; $ j
- {
- If ($ style = 'bts') $ op = $ arr [$ I] <$ arr [$ j];
- Else if ($ style = 'stab') $ op = $ arr [$ I]> $ arr [$ j];
- If ($ op)
- {
- $ Temp = $ arr [$ I];
- $ Arr [$ I] = $ arr [$ j];
- $ Arr [$ j] = $ temp;
- $ Flag = true;
- }
- }
- If ($ flag = false)
- {
- Break;
- }
- }
- Foreach ($ arr as $ key => $ value)
- {
- Echo $ value .',';
- }
- }
- $ Arr1 = array (21.5, 7 );
- SelectSort ($ arr1, 'stab ');
-
Function selectSort ($ arr, $ style)
- {
- $ Temp = 0;
- $ Flag = false;
- For ($ I = 0; $ I
- {
- For ($ j = $ I + 1; $ j
- {
- If ($ style = 'bts') $ op = $ arr [$ I] <$ arr [$ j];
- Else if ($ style = 'stab') $ op = $ arr [$ I]> $ arr [$ j];
- If ($ op)
- {
- $ Temp = $ arr [$ I];
- $ Arr [$ I] = $ arr [$ j];
- $ Arr [$ j] = $ temp;
- $ Flag = true;
- }
- }
- If ($ flag = false)
- {
- Break;
- }
- }
- Foreach ($ arr as $ key => $ value)
- {
- Echo $ value .',';
- }
- }
- $ Arr1 = array (21.5, 7 );
- SelectSort ($ arr1, 'stab ');
- Echo"
";
- ?>
|