-
Notifications
You must be signed in to change notification settings - Fork 127
/
cocktail-sort.c
58 lines (56 loc) · 1.09 KB
/
cocktail-sort.c
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
// Alogrithm for Cocktail Sort to sort element in ascending order
#include <stdio.h>
int main(void)
{
int i, j, start=0, n, end, flag;
printf ("Enter value of n: ");
scanf ("%d", &n);
int a[n];
printf ("Please enter %d values\n",n);
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
flag = 0;
end = n;
while(start <= end)
{
for(i=start; i<end-1; i++)
{
if(a[i]>a[i+1])
{
int temp =a[i];
a[i] = a[i+1];
a[i+1] = temp;
flag=1;
}
}
if(flag==0)
{
break;
}
flag=0;
end = end-1;
for(i=end; i>start-1; i--)
{
if(a[i]<a[i-1])
{
int temp = a[i];
a[i] = a[i-1];
a[i-1]= temp;
flag=1;
}
}
if(flag==0)
{
break;
}
start = start+1;
flag=0;
}
for(i=0; i<n; i++)
{
printf("%d ", a[i] );
}
return 0;
}