雞尾酒排序

雞尾酒排序（Cocktail shaker sort），亦為定向冒泡排序，雞尾酒攪拌排序，攪拌排序（也可以視作選擇排序的一種變形），漣漪排序，來回排序或快樂小時排序，是冒泡排序的一種變形。此演算法與冒泡排序的不同處在於排序時是以雙向在序列中進行排序。

**偽代碼**
將一個序列由小到大進行排序：
function cocktail_sort(list, list_length){ // the first element of list has index 0
bottom = 0;
top = list_length - 1;
swapped = true; 
while(swapped == true) // if no elements have been swapped, then the list is sorted
{
swapped = false; 
for(i = bottom; i  listi + 1)  // test whether the two elements are in the correct order
{
swap(listi, listi + 1); // let the two elements change places
swapped = true;
}
}
// decreases top the because the element with the largest value in the unsorted
// part of the list is now on the position top 
top = top - 1; 
for(i = top; i > bottom; i = i - 1)
{
if(listi // increases bottom because the element with the smallest value in the unsorted 
// part of the list is now on the position bottom 
bottom = bottom + 1;  
}
}

**與冒泡排序不同的地方**
雞尾酒排序等於是冒泡排序的輕微變形。不同的地方在於從低到高然後從高到低，而冒泡排序則僅從低到高去比較序列裡的每個元素。他可以得到比冒泡排序稍微好一點的效能，原因是冒泡排序只從一個方向進行比對（由低到高），每次循環只移動一個項目。
以序列（2,3,4,5,1）為例，雞尾酒排序只需要訪問一次序列就可以完成排序，但如果使用冒泡排序則需要四次。但是在亂數序列的狀態下，雞尾酒排序與冒泡排序的效率與其他眾多排序算法相比均比較低。

**實作範例**

**C語言**
void cocktail_sort(int arr, int len) {
	int i, left = 0, right = len - 1;
	int temp;
	while (left  arri + 1) {
				temp = arri;
				arri = arri + 1;
				arri + 1 = temp;
			}
		right--;
		for (i = right; i > left; i--)
			if (arri - 1 > arri) {
				temp = arri;
				arri = arri - 1;
				arri - 1 = temp;
			}
		left++;
	}
}

**C++**
template //整數或浮點數皆可使用,若要使用物件(class)時必須設定大於(>)的運算子功能
void cocktail_sort(T arr, int len) {
	int j, left = 0, right = len - 1;
	while (left  arrj + 1)
				swap(arrj, arrj + 1);
		right--;
		for (j = right; j > left; j--)
			if (arrj - 1 > arrj)
				swap(arrj - 1, arrj);
		left++;
	}
}

**Rust**
fn cocktail_sort(arr: &mut T) {
let mut bottom: usize = 0;
let mut top = arr.len() - 1;
let mut swapped = true;
while swapped {
swapped = false;
for i in bottom..top {
if arri > arri+1 {
arr.swap(i, i+1);
swapped = true;
}
}
top -= 1;
for j in ((bottom + 1)..=top).rev() {
if arrj 

**JAVA**
public static void cocktail_sort(int arr) {	
	int i, left = 0, right = arr.length - 1;
	int temp;
	while (left  arri + 1) {
				temp = arri;
				arri = arri + 1;
				arri + 1 = temp;
			}
		right--;
		for (i = right; i > left; i--)
			if (arri - 1 > arri) {
				temp = arri;
				arri = arri - 1;
				arri - 1 = temp;
			}
		left++;
	}
}

**JavaScript**
Array.prototype.cocktail_sort = function() {
	var i, left = 0, right = this.length - 1;
	var temp;
	while (left  thisi + 1) {
				temp = thisi;
				thisi = thisi + 1;
				thisi + 1 = temp;
			}
		right--;
		for (i = right; i > left; i--)
			if (thisi - 1 > thisi) {
				temp = thisi;
				thisi = thisi - 1;
				thisi - 1 = temp;
			}
		left++;
	}
};

**PHP**
function swap(&$x, &$y) {
	$t = $x;
	$x = $y;
	$y = $t;
}
function cocktail_sort(&$arr) {//php的陣列視為基本型別，所以必須用傳參才能修改原陣列
	$left = 0;
	$right = count($arr) - 1;
	while ($left  $arr$j + 1)
				swap($arr$j, $arr$j + 1);
		$right--;
		for ($j = $right; $j > $left; $j--)
			if ($arr$j - 1 > $arr$j)
				swap($arr$j - 1, $arr$j);
		$left++;
	}
}

**Python 2.7**
def cocktail_sort(l):
l_len = len(l)
for i in range(l_len, 0, -1):
rem_i_l_len = abs(i - l_len)
isNeedContinue = False
obverse_count = len(lrem_i_l_len : i-1)
reverse_count = len(lrem_i_l_len + 1 : i-1)

for j in range(obverse_count):
if lj > lj + 1:
lj, lj + 1 = lj + 1, lj
isNeedContinue = True
# you can print this to observe the whole process
# print l

for j in range(reverse_count, 0, -1):
if lj 

**Python 3.10**
def cocktail_sort(arr: list, bottom: int = None, top: int = None):
if not bottom and not top:
bottom, top = 0, len(arr) - 1
if bottom == top or bottom > top:
return
swapped: bool = False
for i in range(bottom, top):
if arri > arri + 1:
arri + 1, arri = arri, arri + 1
swapped = True
for i in range(top - 1, bottom, -1):
if arri 

**Golang**
func cocktailSort(arr int) {
	left := 0
	right := len(arr) - 1
	for left  arri+1 {
				arri, arri+1 = arri+1, arri
			}
		}
		right--
		for i := right; i > left; i-- {
			if arri-1 > arri {
				arri-1, arri = arri, arri-1
			}
		}
		left++
	}
}

**Julia (程式語言)**
# Julia Sample : CocktailSort
function CocktailSort(A)
	isordered, lo, hi  = false, 1, length(A)

	while !isordered && hi > lo
		isordered = true
		
		for i=lo+1:hi
			if Ai  Ai
				Ai-1, Ai = Ai, Ai-1
				isordered = false
			end
		end
		lo += 1
	end
	return A
end
# Main Code
A = 16,586,1,31,354,43,3
println(A)                      # Original Array
println(CocktailSort(A)) 		# Cocktail Sort Array

**複雜度**
雞尾酒排序最糟或是平均所花費的次數都是，但如果序列在一開始已經大部分排序過的話，會接近。

**外部連結**
 Java source code and an animated demo of cocktail sort (called bi-directional bubble sort) and several other algorithms
*Implementations of cocktail sort in several different programming languages