-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathcyclic_sort.java
40 lines (36 loc) · 1.06 KB
/
cyclic_sort.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
29
30
31
32
33
34
35
36
37
38
39
40
/*
Cylic Sort is a sorting algorithm ideally used if the question
reads "given range of numbers from 0 to N or 1 to N"
*/
/*
Approach used :
Check if the number is in the correct index, if not swap, and then continue iterating
*/
import java.util.*;
public class cyclic_sort{
//swapping
public static void swap(int[] arr, int first, int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
public static void cycleSort(int[] arr){
int i = 0;
while(i < arr.length){
int correctIndex = arr[i] - 1;
/*if the question reads 1 to N, correctIndex = arr[i] - 1;
if the question reads from 0 to N, correctIndex = arr[i]; */
if(arr[i] != arr[correctIndex]){
swap(arr, i, correctIndex);
}
else{
i++;
}
}
}
public static void main(String[] args){
int[] arr = {3, 5, 2, 1, 4};
cycleSort(arr);
System.out.println(Arrays.toString(arr));
}
}