Added to Favorites

Related Searches

Nearby Words

Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuttle sort or happy hour sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from bubble sort in that sorts in both directions each pass through the list. This sorting algorithm is only marginally more difficult than bubble sort to implement, and solves the problem with so-called turtles in bubble sort.

procedure cocktailSort(A : list of sortable items ) defined as:

` do`

swapped := false

for each i in 0 to length(A ) - 2 do:

if A[i ] > A[i + 1 ] then // test whether the two elements are in the wrong order

` swap(A[i ], A[i + 1 ] ) // let the two elements change places`

swapped := true

` end if`

` end for`

if swapped = false then

` // we can exit the outer loop here if no swaps occurred.`

` break do-while loop`

` end if`

swapped := false

for each i in length(A ) - 2 to 0 do:

if A[i ] > A[i + 1 ] then

swap(A[i ], A[i + 1 ] )

swapped := true

` end if`

` end for`

while swapped // if no elements have been swapped, then the list is sorted

`end procedure`

The first rightward pass will shift the largest element to its correct place at the end, and the following leftward pass will shift the smallest element to its correct place at the beginning. The second complete pass will shift the second largest and second smallest elements to their correct places, and so on. After i passes, the first i and the last i elements in the list are in their correct positions, and do not need to be checked. By shortening the part of the list that is sorted each time, the number of operations can be halved (see bubble sort).

procedure oddEvenSort(A : list of sortable items ) defined as:

` // `begin` and `end` marks the first and last index to check`

begin := -1

end := length(A ) - 2

` do`

swapped := false

` // increases `begin` because the elements before `begin` are in correct order`

begin := begin + 1

for each i in begin to end do:

if A[i ] > A[i + 1 ] then

swap(A[i ], A[i + 1 ] )

swapped := true

` end if`

` end for`

if swapped = false then

` break do-while loop`

` end if`

swapped := false

` // decreases `end` because the elements after `end` are in correct order`

end := end - 1

for each i in end to begin do:

if A[i ] > A[i + 1 ] then

swap(A[i ], A[i + 1 ] )

swapped := true

` end if`

` end for`

` while swapped`

`end procedure`

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.

- Java source code and an animated demo of cocktail sort (called bi-directional bubble sort) and several other algorithms
- .NET Implementation of cocktail sort and several other algorithms

Wikipedia, the free encyclopedia © 2001-2006 Wikipedia contributors (Disclaimer)

This article is licensed under the GNU Free Documentation License.

Last updated on Sunday August 10, 2008 at 18:52:17 PDT (GMT -0700)

View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation

This article is licensed under the GNU Free Documentation License.

Last updated on Sunday August 10, 2008 at 18:52:17 PDT (GMT -0700)

View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation

Copyright © 2014 Dictionary.com, LLC. All rights reserved.