-
Notifications
You must be signed in to change notification settings - Fork 5
/
CyclicSort.java
28 lines (24 loc) · 946 Bytes
/
CyclicSort.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
import java.util.Arrays;
public class CyclicSort {
public static void main(String[] args) {
// Example array: [5, 4, 3, 2, 1]
int[] nums = {5, 4, 3, 2, 1};
sortCyclic(nums);
System.out.println(Arrays.toString(nums));
}
static void sortCyclic(int[] arr) {
int i = 0;
// Continue the process until we have examined all elements in the array.
while (i < arr.length) {
int correct = arr[i] - 1; // Calculate the correct index for the current element.
// If the current element is not in its correct position.
if (arr[i] != arr[correct]) {
int temp = arr[i];
arr[i] = arr[correct];
arr[correct] = temp;
} else {
i++; // Move to the next element as it is already in its correct position.
}
}
}
}