-
Notifications
You must be signed in to change notification settings - Fork 364
/
Cyclic_Sort.c
59 lines (53 loc) · 1.07 KB
/
Cyclic_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
59
/*
* C Program to Implement Cyclic Sort
*/
#include <stdio.h>
#define MAX 8
void cyclic_sort(int *);
void main()
{
int a[MAX],i;
printf("enter the elements into array :");
for (i = 0;i < MAX; i++)
{
scanf("%d", &a[i]);
}
cyclic_sort(a);
printf("sorted elements are :\n");
for (i = 0;i < MAX; i++)
{
printf("%d ", a[i]);
}
}
/* sorts elements using cyclic sort algorithm */
void cyclic_sort(int * a)
{
int temp, item, pos, i, j, k;
for (i = 0;i < MAX; i++)
{
item = a[i];
pos = i;
do
{
k = 0;
for (j = 0;j < MAX;j++)
{
if (pos != j && a[j] < item)
{
k++;
}
}
if (pos != k)
{
while (pos != k && item == a[k])
{
k++;
}
temp = a[k];
a[k] = item;
item = temp;
pos = k;
}
}while (pos != i);
}
}