forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle_Sort.c
76 lines (71 loc) · 1.06 KB
/
Circle_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include<stdlib.h>
#include <stdbool.h>
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
bool circle_sort_rec(int *arr,int n)
{
bool swaped =false;
int mid,i;
if(n<=2)
{
if(arr[0]>arr[n-1])
{
swap(&arr[0],&arr[n-1]);
swaped = true;
}
return swaped;
}
mid = (n+1)/2;
for(i=0;i<mid;i++)
{
if(i == n-i-1)
{
if(arr[i]>arr[i+1])
{
swap(&arr[i],&arr[i+1]);
swaped = true;
}
}
else
{
if(arr[i]>arr[n-i-1])
{
swap(&arr[i],&arr[n-i-1]);
swaped = true;
}
}
}
if(circle_sort_rec(arr,mid))
swaped = true;
if(circle_sort_rec(arr+mid,n-mid))
swaped =true;
return swaped;
}
void circle_sort(int *arr,int size)
{
while(circle_sort_rec(arr,size))
{
;
}
return;
}
void main()
{
int *arr;
int size,i;
printf("Enter the size of the array : ");
scanf("%d", &size);
arr = (int *)malloc(size * sizeof(int));
printf("Enter the elements of the array : ");
for(i=0;i<size;i++)
scanf("%d",arr+i);
circle_sort(arr,size);
for(i=0;i<size;i++)
printf("%d ", arr[i]);
free(arr);
}