MCPcopy Create free account
hub / github.com/SR-Sunny-Raj/Hacktoberfest2021-DSA / CycleSort

Class CycleSort

06. Sorting/CycleSortInJava.java:2–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.Arrays;
2public class CycleSort {
3 public static void main(String[] args) {
4
5 int[] arr= {5,4,3,2,1};
6 cycleSort(arr);
7 System.out.println(Arrays.toString(arr));
8
9 }
10 static void cycleSort(int[] arr)
11 {
12 int i = 0;
13 while (i < arr.length)
14 {
15 int correct= arr[i]-1;
16 if(arr[i]!=arr[correct])
17 swap(arr,i,correct);
18 else
19 i++;
20 }
21
22 }
23 static void swap(int[] arr,int a, int b)
24 {
25 int temp;
26 temp=arr[a];
27 arr[a]=arr[b];
28 arr[b]=temp;
29 }
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected