Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- int [] myArray = cyclicSort(new int[] {3,4,6,2,1,5});
- for(int i=0; i <myArray.length; i++){
- System.out.println(myArray[i]);
- }
- }
- public static int [] cyclicSort(int a[]){
- int i = 0 ;
- while(i<a.length) {
- // 0 1 2 3 4 5
- // 3,4,6,2,1,5
- // 6 4 3 2 1 5
- // 5 4 3 2 1 6
- // 1 4 3 2 5 6
- // 1 2 3 4 5 6
- // i = 0; || i = 0; || i = 1 | i = 2
- int j = a[i]-1; //j = 2 || j = 5 || j = 4 || j = 0 || j = 3 || j = 1 | j = 2
- if (a[i] != a[j]) { //0 != 6 || 6 != 5 || 5 != 1 || 1!= 1 || 4!= 2 || 2 == 2 | 3 = 3 end
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- } else {
- i++;
- }
- }
- return a;
- }
- }
Add Comment
Please, Sign In to add comment