-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample11.java
70 lines (50 loc) · 1.56 KB
/
Example11.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package applications.algorithms;
import java.util.ArrayList;
/** Category: Algorithms
* ID: Example11
* Description: Partition an array into odds and evens
* Taken From: Question 45 Coding Interviews Questions, Analysis and Solutions
* Details:
* TODO
*/
public class Example11 {
public static void partition(ArrayList<Integer> list){
if(list == null || list.size() <= 1){
return;
}
int start = 0;
int end = list.size() - 1;
while(start < end){
// move start forward until it finds an even
while(start < end && (list.get(start) & 0x1) != 0){
start++;
}
// move end it finds an odd
while(start < end && (list.get(end) & 0x1) == 0){
end--;
}
if(start < end){
int tmp = list.get(start);
list.set(start, list.get(end));
list.set(end, tmp);
}
}
}
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i< 15; ++i){
list.add(i);
}
System.out.println("List before partitioning\n");
for(int i=0; i<list.size(); ++i){
System.out.print(i + " , ");
}
System.out.println("\n");
Example11.partition(list);
System.out.println("List after partitioning\n");
for(int i=0; i<list.size(); ++i){
System.out.print(list.get(i) + " , ");
}
System.out.println("\n");
}
}