Skip to content

Commit 4fa13fa

Browse files
solves sort by parity II
1 parent 2fc5fb2 commit 4fa13fa

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/SortArrayByParityII.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class SortArrayByParityII {
2+
public static int[] sortArrayByParityII(int[] array) {
3+
for (int i = 0, j = 1; i < array.length ; i += 2) {
4+
if ((array[i] & 1) == 1) {
5+
while (j < array.length && ((array[j] & 1) == 1)) j += 2;
6+
swap(array, i, j);
7+
}
8+
}
9+
return array;
10+
}
11+
12+
private static void swap(int[] array, int i, int j) {
13+
int temp = array[i];
14+
array[i] = array[j];
15+
array[j] = temp;
16+
}
17+
}

0 commit comments

Comments
 (0)