-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathCyclicSort.java
More file actions
48 lines (34 loc) · 1.27 KB
/
Copy pathCyclicSort.java
File metadata and controls
48 lines (34 loc) · 1.27 KB
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
package Sorting;
import java.util.Arrays;
public class CyclicSort {
public static void main(String[] args) {
//When given numbers from range 1 to n use cyclic sort..
//Example when you have given question from range 1 to n.
//Like if we have given a sorted array we use binary search likewise if we have given range like 0 to n then we use cyclic sort.
//Index value in every sorted array == index = value-1..
// checkk with first element and then swap it by its correct index
//The time complexity for this is O(n)
//Check swap move.
//not work for negative numbers.
int[] arr = {6,2,1,3,4,5};
Sort(arr);
System.out.println(Arrays.toString(arr));
}
static void Sort(int[] arr){
int indexValue=0;
while (indexValue<arr.length){
int correctIndex = arr[indexValue]-1;
if(arr[indexValue]!=arr[correctIndex]){
swap(arr,indexValue,correctIndex);
} //have to do question missing number in leetcode today.
else {
indexValue++;
}
}
}
static void swap(int[] arr, int first,int second){
int temp= arr[first];
arr[first] = arr[second];
arr[second]=temp;
}
}