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


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.