MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / CyclicSort

Class CyclicSort

Java/SortingTechniques/CyclicSort.java:1–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1public class CyclicSort {
2 public static void main(String[] args) {
3 //test case
4 int[] arr = {3, 1, 5, 4, 2};
5 cycleSort(arr);
6 for (int i = 0; i < arr.length; i++) {
7 System.out.print(arr[i] + " ");
8 }
9 }
10 //cycle sort
11 public static void cycleSort(int[] arr){
12 int i = 0;
13 while(i < arr.length){
14 int correct = arr[i] - 1;
15 if(arr[i] != arr[correct]){
16 swap(arr, i, correct);
17 }else{
18 i++;
19 }
20 }
21 }
22 //swap function
23 public static void swap(int[] arr, int first, int second){
24 int temp = arr[first];
25 arr[first] = arr[second];
26 arr[second] = temp;
27 }
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected