-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Cocktail_shaker_sort.cpp
74 lines (60 loc) · 1.18 KB
/
Cocktail_shaker_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// C++ Program for implementation of Cocktail shaker sort.
#include <bits/stdc++.h>
using namespace std;
// function to sort the array using Cocktail sort
void Cocktail_shaker_sort(int a[], int n)
{
int initial = 0;
int end = n - 1;
bool flag = true;
while (flag)
{
flag = false;
// loop from left to right
for (int i = initial; i < end; ++i)
{
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
flag = true;
}
}
// if nothing moved, then array is sorted
if (!flag)
break;
flag = false;
--end;
// comparing from right to left
for (int i = end - 1; i >= initial; --i)
{
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
flag = true;
}
}
++initial;
}
}
// Function to print the sorted array
void Print(int a[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
// Main function
int main()
{
int no_element;
cout << "Enter the no of element to be sorted: ";
cin >> no_element;
int a[no_element];
// taking input
for(int x=0; x < no_element; x++){
cin>>a[x];
}
int n = sizeof(a) / sizeof(a[0]);
Cocktail_shaker_sort(a, n);
printf("The Sorted array :\n");
Print(a, n);
return 0;
}