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.