A. Internal sorting (directly loaded to memory for sorting): including swap sorting (bubble and quick), 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]
Copy codeThe 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 <count ($ arr)-1; $ I ++)
{
For ($ j = 0; $ j <count ($ arr)-1-$ I; $ 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]
Copy codeThe Code is as follows:
Function selectSort ($ arr, $ style)
{
$ Temp = 0;
$ Flag = false;
For ($ I = 0; $ I <count ($ arr)-1; $ I ++)
{
For ($ j = $ I + 1; $ j <count ($ arr); $ 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 ');
Copy codeThe Code is as follows:
Function selectSort ($ arr, $ style)
{
$ Temp = 0;
$ Flag = false;
For ($ I = 0; $ I <count ($ arr)-1; $ I ++)
{
For ($ j = $ I + 1; $ j <count ($ arr); $ 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 "<br/> ";