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:

    The extent to which a parent is able to see a child’s world through that child’s eyes depends very much on the parent’s ability to appreciate the differences between herself and her child and to respect those differences. Your own children need you to accept them for who they are, not who you would like them to be.
    Lawrence Balter (20th century)

    Then a soldier,
    Full of strange oaths, and bearded like the pard,
    Jealous in honor, sudden, and quick in quarrel,
    Seeking the bubble reputation
    Even in the cannon’s mouth.
    William Shakespeare (1564–1616)

    Would you convey my compliments to the purist who reads your proofs and tell him or her that I write in a sort of broken-down patois which is something like the way a Swiss waiter talks, and that when I split an infinitive, God damn it, I split it so it will stay split, and when I interrupt the velvety smoothness of my more or less literate syntax with a few sudden words of bar- room vernacular, that is done with the eyes wide open and the mind relaxed but attentive.
    Raymond Chandler (1888–1959)