Created
August 28, 2017 03:25
-
-
Save thmain/8d7cf570a6b89eed6bdd129af6dceb9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class OddAndEven { | |
public static int[] arrange(int [] arrA){ | |
int left =0; | |
int right = arrA.length-1; | |
while(left<right){ | |
if(arrA[left]%2==0) | |
left++; | |
else if(arrA[right]%2==1) | |
right--; | |
else{ | |
//swap left and right elements | |
int temp = arrA[left]; | |
arrA[left] = arrA[right]; | |
arrA[right] = temp; | |
left++; | |
right--; | |
} | |
} | |
return arrA; | |
} | |
public static void main(String[] args) { | |
int [] arrA = {1,2,3,4,6,8,7,12}; | |
System.out.println("Rearranging arrays using left and right indexes"); | |
arrA = arrange(arrA); | |
System.out.println(Arrays.toString(arrA)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment