-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicSorting.java
More file actions
30 lines (25 loc) · 883 Bytes
/
Copy pathCyclicSorting.java
File metadata and controls
30 lines (25 loc) · 883 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
28
29
30
package Sorting;
import java.util.Arrays;
public class CyclicSorting {
public static void main(String[] args) {
int[] arr = {3, 5, 2, 1, 4};
cyclicSorting(arr);
System.out.println(Arrays.toString(arr));
}
static void cyclicSorting(int[] arr){
int i = 0;
while (i < arr.length){
int correct = arr[i] - 1; // it is stating that correct index of the element is its (value - 1) (When Range starts from 1 to n)
if (arr[i] != arr[correct]) { // if the value of element is not according to value of current index
swap(arr, i, correct); // then swap those elements.
}else{
i++;
}
}
}
static void swap(int[] arr, int first, int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}