forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcocktailSort.cpp
36 lines (36 loc) · 939 Bytes
/
cocktailSort.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
#include<iostream>
using namespace std;
void cocktailSort(int arr[], int n){
bool flag = true;
int start = 0, end = n-1;
while(flag){
flag = false;
for(int i = start; i<end; i++){ //scan from left to right as bubble sort
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
flag = true;
}
}
if(!flag){ //if nothing has changed simply break the loop
break;
}
flag = false;
end--; //decrease the end pointer
for(int i = end - 1; i >= start; i--){ //scan from right to left
if(arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
flag = true;
}
}
start++;
}
}
main() {
int data[] = {54, 74, 98, 154, 98, 32, 20, 13, 35, 40};
int n = sizeof(data)/sizeof(data[0]);
cout << "Sorted Sequence ";
cocktailSort(data, n);
for(int i = 0; i <n;i++){
cout << data[i] << " ";
}
}