-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathCycleSort.java
More file actions
51 lines (43 loc) · 1.63 KB
/
CycleSort.java
File metadata and controls
51 lines (43 loc) · 1.63 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
49
50
51
//What is Cycle sort
//When we are given numbers from 1 to N (not necessarily exactly from 1 to N) we use cycle sort.
//It sorts the entire array in one single pass.
//The basic idea behind cycle sort is the sorted array will contain elements on their correct index.
//For eg. - elements from 1 to 5 will be on index 0 to 4 in the sorted array.
//Advantages are :- It sorts the array in-place and does not require additional memory for temporary variables.
//Here is the link for leetcode question which is solved using cycle sort.
//https://leetcode.com/problems/missing-number/
//Here is the link for leetcode question which is solved using cycle sort.
//https://leetcode.com/problems/first-missing-positive/description/
public class CycleSort {
public static void swap(int arr[],int start,int end)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
}
public static void cycleSort(int arr[])
{
int i=0;
while(i<arr.length)
{
int correctIndex=arr[i]-1; //correctIndex is the index that has the value equal to correctIndex-1.
if(arr[i]==arr[correctIndex]) //if value of index i is equal to the value at correctIndex i.e., equal to (value of index i) -1.
//index number (arr[i]) should contain (arr[i]-1)
{
i++;
}
else
{
swap(arr,i,correctIndex);
}
}
for(int x:arr)
{
System.out.print(x+" ");
}
}
public static void main(String[] args) {
int arr[]={3,5,2,1,4};
cycleSort(arr);
}
}