-
Notifications
You must be signed in to change notification settings - Fork 2
/
cocktail.c
55 lines (50 loc) · 1.44 KB
/
cocktail.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
/*
* C Program to Implement CockTail Sort
*/
#include <stdio.h>
#define MAX 8
int main()
{
int data[MAX];
int i, j, n, c;
printf("\nEnter the data");
for (i = 0; i < MAX; i++)
{
scanf("%d", &data[i]);
}
n = MAX;
do
{
/*
* Rightward pass will shift the largest element to its correct place at the end
*/
for (i = 0; i < n - 1; i++)
{
if (data[i] > data[i + 1])
{
data[i] = data[i] + data[i + 1];
data[i + 1] = data[i] - data[i + 1];
data[i] = data[i] - data[i + 1];
}
}
n = n - 1;
/*
* Leftward pass will shift the smallest element to its correct place at the beginning
*/
for (i= MAX - 1, c = 0; i >= c; i--)
{
if(data[i] < data[i - 1])
{
data[i] = data[i] + data[i - 1];
data[i - 1] = data[i] - data[i - 1];
data[i] = data[i] - data[i - 1];
}
}
c = c + 1;
} while (n != 0 && c != 0);
printf("The sorted elements are:");
for (i = 0; i < MAX; i++)
{
printf("%d\t", data[i]);
}
}