Php bubble sorting algorithm-based deep understanding. The basic idea of exchanging sorting: compares the data to be sorted in two or two pairs. if a reverse Order occurs, it is exchanged until all the data is sorted. The basic idea of bubble sorting: 1. The basic idea of switching sorting from the back to the back: compare the data to be sorted in pairs. if there is a reverse order, it is exchanged until all the data is sorted.
• Basic idea of bubble sorting:
1. scan all data from the back to the back. if two adjacent numbers are in reverse order, they are exchanged. -- 1st TB bubble
2. scan the last to 2nd data records from the back to the back. if two adjacent numbers are in reverse order, they are exchanged. -- 2nd TB bubble
3. perform this operation in sequence until n-1 TDE bubbles are performed, or if there is no reverse order in a TDE bubble, the process can be completed in advance.
The code is as follows:
Script
Var arr = [15, 8, 7, 9, 10, 0];
Var _ len = arr. length;
Alert ("before sorting:" + arr );
Var exchange = 0;
Var temp = 0;
For (var I = 0; I {
Exchange = 0;
For (var j = arr. length; j> = I; j --)
{
If (arr [j] <arr [I])
{
Temp = arr [j];
Arr [j] = arr [I];
Arr [I] = temp;
Exchange = 1;
}
}
If (exchange = 0)
{
Break;
}
}
Alert ("sorted:" + arr );
Script
Bytes. Basic idea of bubble sorting: 1. start from the back...