forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle sort.java
89 lines (75 loc) · 1.71 KB
/
Circle sort.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
// Java implementation of Circle Sort
import java.io.*;
import java.util.*;
class GFG {
// Java function to swap the elements of an array
public static void swap(int[] arr, int i, int j) {
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
// Function to perform circular swaps recursively
// This function returns true if there was a swap
// operation performed.
public static boolean circleSortRec(int a[], int low, int high)
{
boolean swapped = false;
// base case
if (low == high)
return false;
// storing the upper and lower bounds
// of list to be used later in the
// recursive case
int lo = low, hi = high;
while (lo < hi)
{
// swaps the pair of elements
// if true
if (a[lo] > a[hi])
{
swap(a,lo,hi);
swapped = true;
}
lo++;
hi--;
}
// special case arises only for list
// of odd size
if (lo == hi)
if (a[lo] > a[hi + 1])
{
swap(a,low,hi+1);
swapped = true;
}
// recursive case to check the
// traverse lists as sub lists
int mid = (high - low) / 2;
boolean firstHalf = circleSortRec(a, low, low+mid);
boolean secondHalf = circleSortRec(a, low+mid+1, high);
return swapped || firstHalf || secondHalf;
}
// This function mainly calls circleSortRec
public static void circleSort(int a[], int n)
{
// Keep calling circleSortRec while
// there is a swap operation.
while (circleSortRec(a, 0, n-1))
{
;
}
}
// Driver code
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]+" ");
}
}
// This code is contributed by Pushpesh Raj