Cocktail Sort - Differences From Bubble Sort

Differences From Bubble Sort

Cocktail sort is a slight variation of bubble sort. It differs in that instead of repeatedly passing through the list from bottom to top, it passes alternately from bottom to top and then from top to bottom. It can achieve slightly better performance than a standard bubble sort. The reason for this is that bubble sort only passes through the list in one direction and therefore can only move items backward one step each iteration.

An example of a list that proves this point is the list (2,3,4,5,1), which would only need to go through one pass of cocktail sort to become sorted, but if using an ascending bubble sort would take four passes. However one cocktail sort pass should be counted as two bubble sort passes. Typically cocktail sort is less than two times faster than bubble sort.

Another optimization can be that the algorithm remembers where the last actual swap has been done. In the next iteration, there will be no swaps beyond this limit and the algorithm has shorter passes. As the Cocktail sort goes bidirectionally, the range of possible swaps, which is the range to be tested, will reduce per pass, thus reducing the overall running time.

Read more about this topic:  Cocktail Sort

Famous quotes containing the words differences, bubble and/or sort:

    I don’t know what immutable differences exist between men and women apart from differences in their genitals; perhaps there are some other unchangeable differences; probably there are a number of irrelevant differences. But it is clear that until social expectations for men and women are equal, until we provide equal respect for both men and women, our answers to this question will simply reflect our prejudices.
    Naomi Weisstein (b. 1939)

    If the bubble reputation can be obtained only at the cannon’s mouth, I am willing to go there for it, provided the cannon is empty.
    Mark Twain [Samuel Langhorne Clemens] (1835–1910)

    What is most appalling in an F. Scott Fitzgerald book is that it is peopleless fiction: Fitzgerald writes about spectral, muscled suits; dresses, hats, and sleeves which have some sort of vague, libidinous throb. These are plainly the product of sickness.
    Edward Dahlberg (1900–1977)