import
java.io.*;
import
java.util.*;
class
GFG {
public
static
void
swap(
int
[] arr,
int
i,
int
j) {
int
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
public
static
boolean
circleSortRec(
int
a[],
int
low,
int
high)
{
boolean
swapped =
false
;
if
(low == high)
return
false
;
int
lo = low, hi = high;
while
(lo < hi)
{
if
(a[lo] > a[hi])
{
swap(a,lo,hi);
swapped =
true
;
}
lo++;
hi--;
}
if
(lo == hi)
if
(a[lo] > a[hi +
1
])
{
swap(a,low,hi+
1
);
swapped =
true
;
}
int
mid = (high - low) /
2
;
boolean
firstHalf = circleSortRec(a, low, low+mid);
boolean
secondHalf = circleSortRec(a, low+mid+
1
, high);
return
swapped || firstHalf || secondHalf;
}
public
static
void
circleSort(
int
a[],
int
n)
{
while
(circleSortRec(a,
0
, n-
1
))
{
;
}
}
public
static
void
main(String[] args)
{
int
a[] = {
6
,
5
,
3
,
1
,
8
,
7
,
2
,
4
};
int
n = a.length;
System.out.print(
"Unsorted : "
);
for
(
int
i=
0
; i<n; i++)
System.out.print(a[i]+
" "
);
circleSort(a, n);
System.out.print(
"\nSorted : "
);
for
(
int
i=
0
; i<n; i++)
System.out.print(a[i]+
" "
);
}
}