forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshakesort.cpp
More file actions
38 lines (38 loc) · 747 Bytes
/
Copy pathshakesort.cpp
File metadata and controls
38 lines (38 loc) · 747 Bytes
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
#include<iostream>
using namespace std;
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void ShakerSort(int a[], int m) {
int i, j, k;
for(i = 0; i < m;) {
for(j = i+1; j < m; j++) {
if(a[j] < a[j-1])
swap(&a[j], &a[j-1]);
}
m--;
for(k = m-1; k > i; k--) {
if(a[k] < a[k-1])
swap(&a[k], &a[k-1]);
}
i++;
}
}
int main() {
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int a[n];
for(i = 0; i < n; i++) {
cout<<"Enter element "<<i+1<<": ";
cin>>a[i];
}
ShakerSort(a, n);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<a[i];
return 0;
}