Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort, ripple sort, shuttle sort or happy hour sort, is a stablesorting algorithm that varies from bubble sort in that instead of repeatedly passing through the list from top to bottom, it passes alternately from top to bottom and then from bottom to top. It can achieve slightly better performance than a standard bubble sort. It is a comparison sort. In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. ... In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. ... Bubble sort, also known as exchange sort, is a simple sorting algorithm. ... A comparison sort is a particular type of sorting algorithm; a number of well-known algorithms are comparison sorts. ...
Note that 'shaker sort' can also refer to a variant of selection sort. Selection sort is a sort algorithm that works as follows: find the minimum value in the list swap it with the value in the first position sort the remainder of the list (excluding the first value) It is probably the most intuitive sort algorithm to invent. ...
The complexity of cocktail sort in Big O notation is O(n²) for a worst case, but becomes closer to O(n) if the list is mostly ordered at the beginning. Big O notation is a mathematical notation used to describe the asymptotic behavior of functions. ...
Implementation
For implementations in real programming languages, see Cocktail sort at Wikisource.