Deep understanding of php Bubble Sorting Algorithm

Source: Internet
Author: User

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>

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.