Some array sorting methods in php _ php skills

Source: Internet
Author: User
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"
";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.