-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle_Sort.cpp
73 lines (65 loc) · 1.04 KB
/
Circle_Sort.cpp
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
#include <bits/stdc++.h>
using namespace std;
bool circle_sort_rec(int *arr,int n)
{
bool swaped =false;
if(n<=2)
{
if(arr[0]>arr[n-1])
{
swap(arr[0],arr[n-1]);
swaped = true;
}
return swaped;
}
int mid = (n+1)/2;
for(int 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;
}
int main()
{
int size;
cout<<"Enter the length of array : ";
cin>>size;
//allocating heap memory for array
int *arr = new int[size];
cout<<"Enter the elements of array : ";
for(int i=0;i<size;i++)
cin>>arr[i];
// calling circle circle sort function/algorithm
circle_sort(arr,size);
// Printing sorted array
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
return 0;
}