forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegregateOddAndEven.java
More file actions
38 lines (30 loc) · 898 Bytes
/
SegregateOddAndEven.java
File metadata and controls
38 lines (30 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.geeksforgeeks.array;
public class SegregateOddAndEven {
public static void main(String[] args) {
int [] arr = {12, 34, 45, 9, 8, 90, 3};
rearrangeInGeeksForGeeksWay(arr);
ArrayRotation.printArray(arr);
}
public static void rearrange(int []arr) {
int low = 0;
int high = 0;
for(int i=0;i<arr.length;i++) {
if(arr[i] % 2 == 0) {
QuickSort.swap(arr,low++,high++);
} else {
high++;
}
}
}
public static void rearrangeInGeeksForGeeksWay(int []arr) {
int low = 0;
int high = arr.length-1;
while (low < high) {
while (low<high && arr[low] % 2 ==0)
low++;
while (high>low && arr[high] % 2 == 1)
high--;
QuickSort.swap(arr,low,high);
}
}
}