Some array sorting methods in php are shared. For more information, see. internal sorting (directly loaded to memory for sorting): Includes swap sorting (bubble and fast), selective sorting, and plug-in sorting
B. External sorting (because the data volume is large, external storage is needed for sorting): including merging and sorting, directly merging and sorting
[Bubble sorting: compares the sorting codes of adjacent elements from the back to the front. if the reverse order is found, it is exchanged. after the end of a round, another round is returned until there is no reverse order for all adjacent numbers, that is, sorting in order]
The code is as follows:
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 arranged]
{
$ 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
[Select sorting: The second number to the nth number are compared with the first number, and the third number to the nth number are compared with the second number, and exchanged until the sorting is complete]
The code is as follows:
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 ');
The code is as follows:
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"
";