import java.util.Arrays;
public class CyclicSort {
public static void main(String[] args) {
int[] arr={3,5,2,1,4};
sort(arr);
System.out.println(Arrays.toString(arr));
}
static void sort(int arr[]){
int i=0;
while(i<arr.length){
int correct=arr[i]-1;
if(arr[i]!=arr[correct]){
swap(arr,i,correct);
}
else{
i++;
}
}
}
static void swap(int[]arr,int first,int second){
int temp=arr[first];
arr[first]=arr[second];
arr[second]=temp;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter