Seperating Order of Even Numbers and After order of Odd numbers with single array in java
import java.util.Arrays;
public class ReArrangeEvenAndOdd {
static void rearrangeEvenAndOdd(int arr[], int n) {
// variables
int j = 0, temp;
// quick sort method
for (int i = 0; i < n; i++) {
// if array of element
// is odd then swap
if (arr[i] % 2 == 0) {
// swap the element
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}
}
public static void main(String[] args) {
int arr[] = {2, 4, 5, 7, 6, 8, 9};
int n = arr.length;
rearrangeEvenAndOdd(arr, n);
System.out.println(Arrays.toString(arr));
}
}
Comments
Post a Comment