Relive the bubble sort of PHP

Source: Internet
Author: User

Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.

It repeatedly visits the column of elements to sort, compares two adjacent elements in turn, and swaps them if their order (e.g., from large to small, the first letter from A to Z) is wrong. The work of a visiting element is repeated until no adjacent elements need to be exchanged, that is, the element has been sorted.
The name of this algorithm is because the larger the elements will slowly "float" to the top of the series (ascending or descending), as the bubbles of carbon dioxide in carbonated drinks will eventually rise to the top, hence the name "bubble sort".

This is the encyclopedia of "bubble sort" interpretation, the basic principle is: Compare the size of the adjacent elements in a set of data, in ascending order, the value of the small forward, the value of the person backward, until all the elements from small to large display. when sorting an array of any length count, the following rule is found: the count-1 wheel needs to be sorted, and each I-wheel needs to be compared count-i times. This uses a double loop, the outer control loop, and the inner layer controls the number of comparisons per round. 

The code is as follows:

技术分享图片

Test code:

技术分享图片

Results:

技术分享图片

Relive the bubble sort of PHP


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.