forked from Anuragcoderealy/Codenewhacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicSort.java
More file actions
27 lines (26 loc) · 700 Bytes
/
CyclicSort.java
File metadata and controls
27 lines (26 loc) · 700 Bytes
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
import java.util.Arrays;
public class CyclicSort {
public static void main(String[] args) {
int[] arr = {3,5,4,1,2};
// cyclic(arr);
CyclicSort ob = new CyclicSort();
ob.cyclic(arr);
System.out.println(Arrays.toString(arr));
}
static void cyclic(int[] arr){
int i = 0;
while(i< arr.length){
int correct = arr[i]-1;
if(arr[i] != arr[correct]){
swap(arr, i,correct);
}else {
i++;
}
}
}
static void swap(int[] arr, int first, int second){
int temp= arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}