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.
• 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.
Copy codeThe 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 <arr. length; 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>