A binary array is an array of 0s and 1s. There are a number of ways to do this. One of them is given below

Start from both ends of the array and swap 1s from left to 0s in right until reach the middle of the array

/**

* Does an in-place sort of the given binary array

*

* @param toSort

*/

```
public static void sortBinaryArray(int[] toSort) {
int begin = 0;
int end = toSort.length - 1;
while (begin < end) {
// If find 0 at left, keep moving
if (0 == toSort[begin]) {
begin++;
}
// if a 1 occurs at left, move from right until find a zero
else if (1 == toSort[end]) {
end--;
}
// Here we found a 1 at left and 0 at right, so swap
else {
toSort[begin] = 0;
toSort[end] = 1;
}
}
}
```

### Driver code

```
public static void main(String[] args) {
int[] bin = new int[] { 1, 1, 0, 0, 0, 1, 1, 1, 1, 1 };
sortBinaryArray(bin);
System.out.println(Arrays.toString(bin));
}
```

### Output

[0, 0, 0, 1, 1, 1, 1, 1, 1, 1]