Posted on by Kalkicode
Code Sorting

Cocktail Sort

Cocktail Sort, also known as Bidirectional Bubble Sort or Shaker Sort, is a variation of the Bubble Sort algorithm. It is a sorting algorithm that sorts a list of items by repeatedly passing through the list in both directions, swapping adjacent items if they are in the wrong order.

The algorithm starts by iterating through the list from the beginning to the end, comparing adjacent items and swapping them if they are in the wrong order. Once the end of the list is reached, the algorithm starts iterating from the end of the list back to the beginning, again swapping adjacent items if they are in the wrong order.

This process is repeated until no more swaps are needed, indicating that the list is now sorted. The name "Cocktail Sort" comes from the fact that the algorithm is like a cocktail shaker, where the items are repeatedly swapped back and forth until they are properly sorted.

Cocktail Sort has a similar time complexity to Bubble Sort, with an average and worst-case time complexity of O(n^2), where n is the number of items in the list. However, it can be slightly more efficient than Bubble Sort in some cases because it can move items to their correct positions faster by iterating in both directions.

Here given code implementation process.

// C Program 
// Sort array elements by using cocktail sort
#include <stdio.h>

//Function which is display arr elements
void display(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}

// Swapping the array elements in given location
void swap_data(int arr[], int first, int second)
{
	int temp = arr[first];
	arr[first] = arr[second];
	arr[second] = temp;
}

//Perform the cocktail sort in given array
void cocktail_sort(int arr[], int size)
{
	//Set initial boundary index
	int n = 0;
	int m = size - 1;
	//Loop controlling variable
	int i = 0;
	int j = 0;
	while (n <= m)
	{
		//Sort pairwise elements from n to m
		for (i = n; i < m; i++)
		{
			if (arr[i] > arr[i + 1])
			{
				swap_data(arr, i, i + 1);
			}
		}
		m--;
		//Sort pairwise elements from m to n
		for (j = m; j > n; j--)
		{
			if (arr[j] < arr[j - 1])
			{
				swap_data(arr, j, j - 1);
			}
		}
		n++;
	}
}
int main()
{
	//Define an sorted arr of integers
	int arr[] = {
		11,
		8,
		3,
		8,
		12,
		-1,
		-3,
		1,
		4,
		0,
		5
	};
	//Get the size of arr
	int size = sizeof(arr) / sizeof(arr[0]);
	//Before sort
	printf("\n  Before Sort  :\n");
	display(arr, size);
	//Sort element
	cocktail_sort(arr, size);
	//After sort
	printf("\n  After Sort  :\n");
	display(arr, size);
	return 0;
}

Output

  Before Sort  :
  11  8  3  8  12  -1  -3  1  4  0  5

  After Sort  :
  -3  -1  0  1  3  4  5  8  8  11  12
/*
	Java Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	//Function which is display arr elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	// Swapping the array elements in given location
	public void swap_data(int[] arr, int first, int second)
	{
		int temp = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	//Perform the cocktail sort in given array
	public void cocktail_sort(int[] arr, int size)
	{
		//Set initial boundary index
		int n = 0;
		int m = size - 1;
		//Loop controlling variable
		int i = 0;
		int j = 0;
		while (n <= m)
		{
			//Sort pairwise elements from n to m
			for (i = n; i < m; i++)
			{
				if (arr[i] > arr[i + 1])
				{
					swap_data(arr, i, i + 1);
				}
			}
			m--;
			//Sort pairwise elements from m to n
			for (j = m; j > n; j--)
			{
				if (arr[j] < arr[j - 1])
				{
					swap_data(arr, j, j - 1);
				}
			}
			n++;
		}
	}
	public static void main(String[] args)
	{
		MySort obj = new MySort();
		//Define an sorted array of integers
		int[] arr = {
			11,
			8,
			3,
			8,
			12,
			-1,
			-3,
			1,
			4,
			0,
			5
		};
		//Get the size of array
		int size = arr.length;
		System.out.print("\n Before Sort :\n");
		obj.display(arr, size);
		//Sort element
		obj.cocktail_sort(arr, size);
		System.out.print("\n After Sort :\n");
		obj.display(arr, size);
	}
}

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
//Include header file
#include <iostream>

using namespace std;
/*
	C++ Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	public:
		//Function which is display arr elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	// Swapping the array elements in given location
	void swap_data(int arr[], int first, int second)
	{
		int temp = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	//Perform the cocktail sort in given array
	void cocktail_sort(int arr[], int size)
	{
		//Set initial boundary index
		int n = 0;
		int m = size - 1;
		//Loop controlling variable
		int i = 0;
		int j = 0;
		while (n <= m)
		{
			//Sort pairwise elements from n to m
			for (i = n; i < m; i++)
			{
				if (arr[i] > arr[i + 1])
				{
					this->swap_data(arr, i, i + 1);
				}
			}
			m--;
			//Sort pairwise elements from m to n
			for (j = m; j > n; j--)
			{
				if (arr[j] < arr[j - 1])
				{
					this->swap_data(arr, j, j - 1);
				}
			}
			n++;
		}
	}
};
int main()
{
	MySort obj = MySort();
	int arr[] = {
		11 , 8 , 3 , 8 , 12 , -1 , -3 , 1 , 4 , 0 , 5
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << "\n Before Sort :\n";
	obj.display(arr, size);
	//Sort element
	obj.cocktail_sort(arr, size);
	cout << "\n After Sort :\n";
	obj.display(arr, size);
	return 0;
}

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
//Include namespace system
using System;
/*
	C# Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	//Function which is display arr elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	// Swapping the array elements in given location
	public void swap_data(int[] arr, int first, int second)
	{
		int temp = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	//Perform the cocktail sort in given array
	public void cocktail_sort(int[] arr, int size)
	{
		//Set initial boundary index
		int n = 0;
		int m = size - 1;
		//Loop controlling variable
		int i = 0;
		int j = 0;
		while (n <= m)
		{
			//Sort pairwise elements from n to m
			for (i = n; i < m; i++)
			{
				if (arr[i] > arr[i + 1])
				{
					swap_data(arr, i, i + 1);
				}
			}
			m--;
			//Sort pairwise elements from m to n
			for (j = m; j > n; j--)
			{
				if (arr[j] < arr[j - 1])
				{
					swap_data(arr, j, j - 1);
				}
			}
			n++;
		}
	}
	public static void Main(String[] args)
	{
		MySort obj = new MySort();
		int[] arr = {
			11 , 8 , 3 , 8 , 12 , -1 , -3 , 1 , 4 , 0 , 5
		};
		//Get the size of array
		int size = arr.Length;
		Console.Write("\n Before Sort :\n");
		obj.display(arr, size);
		//Sort element
		obj.cocktail_sort(arr, size);
		Console.Write("\n After Sort :\n");
		obj.display(arr, size);
	}
}

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
<?php
/*
	Php Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	//Function which is display arr elements
	public	function display( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	// Swapping the array elements in given location
	public	function swap_data( & $arr, $first, $second)
	{
		$temp = $arr[$first];
		$arr[$first] = $arr[$second];
		$arr[$second] = $temp;
	}
	//Perform the cocktail sort in given array
	public	function cocktail_sort( & $arr, $size)
	{
		//Set initial boundary index
		$n = 0;
		$m = $size - 1;
		//Loop controlling variable
		$i = 0;
		$j = 0;
		while ($n <= $m)
		{
			//Sort pairwise elements from n to m
			for ($i = $n; $i < $m; $i++)
			{
				if ($arr[$i] > $arr[$i + 1])
				{
					$this->swap_data($arr, $i, $i + 1);
				}
			}
			$m--;
			//Sort pairwise elements from m to n
			for ($j = $m; $j > $n; $j--)
			{
				if ($arr[$j] < $arr[$j - 1])
				{
					$this->swap_data($arr, $j, $j - 1);
				}
			}
			$n++;
		}
	}
}

function main()
{
	$obj = new MySort();
	//Define an sorted array of integers
	$arr = array(11, 8, 3, 8, 12, -1, -3, 1, 4, 0, 5);
	//Get the size of array
	$size = count($arr);
	echo "\n Before Sort :\n";
	$obj->display($arr, $size);
	//Sort element
	$obj->cocktail_sort($arr, $size);
	echo "\n After Sort :\n";
	$obj->display($arr, $size);
}
main();

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
/*
	Node Js Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	//Function which is display arr elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Swapping the array elements in given location
	swap_data(arr, first, second)
	{
		var temp = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	//Perform the cocktail sort in given array
	cocktail_sort(arr, size)
	{
		//Set initial boundary index
		var n = 0;
		var m = size - 1;
		//Loop controlling variable
		var i = 0;
		var j = 0;
		while (n <= m)
		{
			//Sort pairwise elements from n to m
			for (i = n; i < m; i++)
			{
				if (arr[i] > arr[i + 1])
				{
					this.swap_data(arr, i, i + 1);
				}
			}
			m--;
			//Sort pairwise elements from m to n
			for (j = m; j > n; j--)
			{
				if (arr[j] < arr[j - 1])
				{
					this.swap_data(arr, j, j - 1);
				}
			}
			n++;
		}
	}
}

function main()
{
	var obj = new MySort();
	//Define an sorted array of integers
	var arr = [11, 8, 3, 8, 12, -1, -3, 1, 4, 0, 5];
	//Get the size of array
	var size = arr.length;
	process.stdout.write("\n Before Sort :\n");
	obj.display(arr, size);
	//Sort element
	obj.cocktail_sort(arr, size);
	process.stdout.write("\n After Sort :\n");
	obj.display(arr, size);
}
main();

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
# 	Python 3 Program
# 	Sort array elements by using cocktail sort

class MySort :
	# Function which is display arr elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print("\n", end = "")
	
	#  Swapping the array elements in given location
	def swap_data(self, arr, first, second) :
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	
	# Perform the cocktail sort in given array
	def cocktail_sort(self, arr, size) :
		# Set initial boundary index
		n = 0
		m = size - 1
		# Loop controlling variable
		i = 0
		j = 0
		while (n <= m) :
			# Sort pairwise elements from n to m
			i = n
			while (i < m) :
				if (arr[i] > arr[i + 1]) :
					self.swap_data(arr, i, i + 1)
				
				i += 1
			
			m -= 1
			# Sort pairwise elements from m to n
			j = m
			while (j > n) :
				if (arr[j] < arr[j - 1]) :
					self.swap_data(arr, j, j - 1)
				
				j -= 1
			
			n += 1
		
	

def main() :
	obj = MySort()
	# Define an sorted array of integers
	arr = [11, 8, 3, 8, 12, -1, -3, 1, 4, 0, 5]
	# Get the size of array
	size = len(arr)
	print("\n Before Sort :\n", end = "")
	obj.display(arr, size)
	# Sort element
	obj.cocktail_sort(arr, size)
	print("\n After Sort :\n", end = "")
	obj.display(arr, size)

if __name__ == "__main__": main()

Output

 Before Sort :
  11  8  3  8  12  -1  -3  1  4  0  5

 After Sort :
  -3  -1  0  1  3  4  5  8  8  11  12
# 	Ruby Program
# 	Sort array elements by using cocktail sort

class MySort

	# Function which is display arr elements
	def display(arr, size)
	
		i = 0
		while (i < size)
		
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
	#  Swapping the array elements in given location
	def swap_data(arr, first, second)
	
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	end
	# Perform the cocktail sort in given array
	def cocktail_sort(arr, size)
	
		# Set initial boundary index
		n = 0
		m = size - 1
		# Loop controlling variable
		i = 0
		j = 0
		while (n <= m)
		
			# Sort pairwise elements from n to m
			i = n
			while (i < m)
			
				if (arr[i] > arr[i + 1])
				
					self.swap_data(arr, i, i + 1)
				end
				i += 1
			end
			m -= 1
			# Sort pairwise elements from m to n
			j = m
			while (j > n)
			
				if (arr[j] < arr[j - 1])
				
					self.swap_data(arr, j, j - 1)
				end
				j -= 1
			end
			n += 1
		end
	end
end
def main()

	obj = MySort.new()
	# Define an sorted array of integers
	arr = [11, 8, 3, 8, 12, -1, -3, 1, 4, 0, 5]
	# Get the size of array
	size = arr.length
	print("\n Before Sort :\n")
	obj.display(arr, size)
	# Sort element
	obj.cocktail_sort(arr, size)
	print("\n After Sort :\n")
	obj.display(arr, size)
end
main()

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
/*
	Scala Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	//Function which is display arr elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Swapping the array elements in given location
	def swap_data(arr: Array[Int], first: Int, second: Int): Unit = {
		var temp: Int = arr(first);
		arr(first) = arr(second);
		arr(second) = temp;
	}
	//Perform the cocktail sort in given array
	def cocktail_sort(arr: Array[Int], size: Int): Unit = {
		//Set initial boundary index
		var n: Int = 0;
		var m: Int = size - 1;
		//Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		while (n <= m)
		{
			//Sort pairwise elements from n to m
			i = n;
			while (i < m)
			{
				if (arr(i) > arr(i + 1))
				{
					swap_data(arr, i, i + 1);
				}
				i += 1;
			}
			m -= 1;
			//Sort pairwise elements from m to n
			j = m;
			while (j > n)
			{
				if (arr(j) < arr(j - 1))
				{
					swap_data(arr, j, j - 1);
				}
				j -= 1;
			}
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MySort = new MySort();
		//Define an sorted array of integers
		var arr: Array[Int] = Array(11, 8, 3, 8, 12, -1, -3, 1, 4, 0, 5);
		//Get the size of array
		var size: Int = arr.length;
		print("\n Before Sort :\n");
		obj.display(arr, size);
		//Sort element
		obj.cocktail_sort(arr, size);
		print("\n After Sort :\n");
		obj.display(arr, size);
	}
}

Output

 Before Sort :
 11 8 3 8 12 -1 -3 1 4 0 5

 After Sort :
 -3 -1 0 1 3 4 5 8 8 11 12
/*
	Swift Program
	Sort array elements by using cocktail sort
*/
class MySort
{
	//Function which is display arr elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Swapping the array elements in given location
	func swap_data(_ arr: inout[Int], _ first: Int, _ second: Int)
	{
		let temp: Int = arr[first];
		arr[first] = arr[second];
		arr[second] = temp;
	}
	//Perform the cocktail sort in given array
	func cocktail_sort(_ arr: inout[Int], _ size: Int)
	{
		//Set initial boundary index
		var n: Int = 0;
		var m: Int = size - 1;
		//Loop controlling variable
		var i: Int = 0;
		var j: Int = 0;
		while (n <= m)
		{
			//Sort pairwise elements from n to m
			i = n;
			while (i < m)
			{
				if (arr[i] > arr[i + 1])
				{
					self.swap_data(&arr, i, i + 1);
				}
				i += 1;
			}
			m -= 1;
			//Sort pairwise elements from m to n
			j = m;
			while (j > n)
			{
				if (arr[j] < arr[j - 1])
				{
					self.swap_data(&arr, j, j - 1);
				}
				j -= 1;
			}
			n += 1;
		}
	}
}
func main()
{
	let obj: MySort = MySort();
	//Define an sorted array of integers
	var arr: [Int] = [11, 8, 3, 8, 12, -1, -3, 1, 4, 0, 5];
	//Get the size of array
	let size: Int = arr.count;
	print("\n Before Sort :");
	obj.display(arr, size);
	//Sort element
	obj.cocktail_sort(&arr, size);
	print("\n After Sort :");
	obj.display(arr, size);
}
main();

Output

 Before Sort :
  11  8  3  8  12  -1  -3  1  4  0  5

 After Sort :
  -3  -1  0  1  3  4  5  8  8  11  12

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment