[Common algorithm PHP implementation] cocktail sorting

Source: Internet
Author: User
The following concepts are taken from Wikipedia: Cocktail sorting, also called oriented Bubble sorting ,? Sort cocktails ,? Stirring sorting is a kind of deformation of Bubble sorting. The difference is that from low to high and then from high to low, while Bubble Sorting only compares each element in the sequence from low to high. It is a little more efficient than the bubble sort, because the bubble sort is only from

The following concepts are taken from Wikipedia: Cocktail sorting, also called oriented Bubble sorting ,? Sort cocktails ,? Stirring sorting is a kind of deformation of Bubble sorting. The difference is that from low to high and then from high to low, while Bubble Sorting only compares each element in the sequence from low to high. It is a little more efficient than the bubble sort, because the bubble sort is only from

The following concepts are taken from Wikipedia:

Cocktail sorting, also called targeted Bubble sorting ,? Sort cocktails ,? Stirring sorting is a kind of deformation of Bubble sorting. The difference is that from low to high and then from high to low, while Bubble Sorting only compares each element in the sequence from low to high. The efficiency is slightly better than that of Bubble Sorting because Bubble Sorting only compares (from low to high) in one direction and only moves one project in each loop.

Taking the sequence (, 1) as an example, the sort of cocktails can be completed only once the sequence is accessed, but it takes four times to use the bubble sort. However, in a disordered number sequence, the efficiency of cocktail sorting and Bubble Sorting is poor. The advantage is that the concept is simple.

The worst sort of cocktails or the average number of consumed cocktails is the same, but if the sequence has been mostly sorted at the beginning, it will be close.

In fact, just call it bidirectional bubble sorting.

// If the code line is long, you can drag the function cocktailSort ($ arr, $ sort = 'asc ') {$ sorted = false; $ bottom = 0; $ top = count ($ arr)-1; while (! $ Sorted) {$ sorted = true; for ($ I = $ bottom; $ I <$ top; $ I ++) {if ($ arr [$ I]> $ arr [$ I + 1] & $ sort = 'asc ') | ($ arr [$ I] <$ arr [$ I + 1] & $ sort = 'desc ')) {$ temp = $ arr [$ I + 1]; $ arr [$ I + 1] = $ arr [$ I]; $ arr [$ I] = $ temp; $ sorted = false; // The elements to be exchanged indicate that the array has not been sorted.} // The result is that the array has the largest (small) value) the value element is already at the top of the array $ top --; for ($ I = $ top; $ I >$ bottom; $ I --) {if ($ arr [$ I] <$ arr [$ I-1] & $ sort = 'asc ') | ($ arr [$ I]> $ arr [$ I-1] & $ sort = 'desc') {$ temp = $ arr [$ I-1]; $ arr [$ I-1] = $ arr [$ I]; $ arr [$ I] = $ temp; $ sorted = false ;}} // $ bottom + 1 is because the element with the smallest (large) value is already at the bottom of the array $ bottom ++;} return $ arr ;}

Original article address: [common algorithm PHP implementation] cocktail sorting. Thank you for sharing it with the original author.

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.