This article provides a detailed analysis of the Bubble Sorting Algorithm in php. For more information, see
This article provides a detailed analysis of the Bubble Sorting Algorithm in php. For more information, see
The basic idea of exchanging sorting: Compares the data to be sorted in two or two ways. If the reverse order occurs, the Hong Kong virtual host is switched 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. In this Order, it can be completed in advance until n-1 bubbles are performed, or when server space and Hong Kong virtual hosts are in a bubble, there is no reverse order.
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