Php-based Bubble sorting algorithm-PHP Tutorial

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

Related Article

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.