# C++ Program for the Cocktail Sort?

C++Server Side ProgrammingProgramming

Cocktail sort is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort also known as bidirectional bubble sort, cocktail shaker sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list.

Input:53421
Output:12345

## Explanation

In Cocktail, sort array will be consist of the unsorted element. Cocktail sort works in both directions on each pass through the list. It sort array using bubble sorts once forwards and backward.

## Example

#include <iostream>
using namespace std;
int main() {
int arr[] = { 5, 3, 4, 2, 1 };
int m=5;
int n, c;
n=m;
do {
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
arr[i] = arr[i] + arr[i + 1];
arr[i + 1] = arr[i] - arr[i + 1];
arr[i] = arr[i] - arr[i + 1];
}
}
n = n - 1;
for (int i=m-1, c = 0; i >= c; i--) {
if(arr[i] < arr[i - 1]) {
arr[i] = arr[i] + arr[i - 1];
arr[i - 1] = arr[i] - arr[i - 1];
arr[i] = arr[i] - arr[i - 1];
}
}
c = c + 1;
}
while (n != 0 && c != 0);
for (int i = 0; i < m; i++) {
cout<< arr[i]<<"\t";
}
}