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 country is fed up with children and their problems. For the first time in history, the differences in outlook between people raising children and those who are not are beginning to assume some political significance. This difference is already a part of the conflicts in local school politics. It may spread to other levels of government. Society has less time for the concerns of those who raise the young or try to teach them.”
—Joseph Featherstone (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 cannons mouth.”
—William Shakespeare (15641616)
“Ah, I fancy it is just the same with most of what you call your emancipation. You have read yourself into a number of new ideas and opinions. You have got a sort of smattering of recent discoveries in various fieldsdiscoveries that seem to overthrow certain principles which have hitherto been held impregnable and unassailable. But all this has only been a matter of intellect, Miss Westsuperficial acquisition. It has not passed into your blood.”
—Henrik Ibsen (18281906)