Skip to content

Commit

Permalink
Merge pull request #63 from HarshVR1947/main
Browse files Browse the repository at this point in the history
Cyclic Sort code in Java
  • Loading branch information
mrsamirr authored Oct 22, 2023
2 parents 6650bf4 + 78b0bdb commit 7b9b23f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions JAVA/Cyclesort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.Arrays;

class Cyclesort{
public static void main(String[] args) {
int[] arr={3,5,2,1,4};
cyclicsort(arr);
System.out.println(Arrays.toString(arr));
}
static void cyclicsort(int[] arr){
int i=0;
while(i<arr.length){
int correct=arr[i]-1;
if(arr[i]!=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;
}
}

0 comments on commit 7b9b23f

Please sign in to comment.