# Segregate 0s and 1s in an array

Given an array with just 0's and 1's arranged randomly, the task is to separate all the 0's from 1's. We can arrange all the 0's followed by 1's or 1's followed by 0's. This tutorial lists all the possible ways to segregate an array of 0's and 1's from the naïve method to efficient ones.

### Method 1: The Naïve method: Sorting the Array

If we sort the Array in ascending order, we'll get an array with 0's followed by 1's. For an array with 1's followed by 0's, we need to sort the Array in descending order.

Here is a sample code following the selection sort algorithm:

Output:

```Enter the size of the Array: 6
Enter the elements: 0 1 1 1 0 0
0 0 0 1 1 1
```

We used two loops here, one to select an element from the Array and the other to find the minimum element in the Array. Hence the time complexity is O(N2).

### Method 2: Traversing the Array two times: Counting

In this method, we'll traverse the Array only twice to reduce the time complexity. But how do we do that? We'll use the first traversal to count the number of 0's in the Array. By subtracting the number of zeroes from the size of the Array, we'll get the number of 1's in the Array. Now, we'll use the second traversal to fill the Array with 0's followed by 1's.

Sample code:

Output:

```Enter the size of the Array: 6
Enter the elements: 0 1 1 0 1 1
The resultant Array: 0 0 1 1 1 1
```

### Method 3: Traversing the Array only once: Two pointers/ variables

In this approach, we use two variables or pointers, one from the beginning and the other from the end of the Array. If we're arranging 0's followed by 1's, we'll keep checking if there are any 0's on the right side and 1's on the left side using the two variables to swap them.

Output:

```Enter the size of the Array: 6
Enter the elements: 1 1 1 0 0 0
The resultant Array: 0 0 0 1 1 1
```

Understanding:

In the above code, we have two variables, a, and b, pointing to the beginning and end of the Array, respectively.

The logic here is,

1. If we find a 0 on the right side and 1 on the left side of the Array, we'll swap the elements.
2. If we find a 0 on the right side and another 0 on the left side, we can't swap them as a 0 on the left side shouldn't be disturbed. Hence, we'll increment the beginning variable to the next position till we find a 1 to swap with the 0 on the right side.
3. It is the same mechanism for 1 on the left side of the Array with no 0 on the right side. We'll decrement the right variable to the preceding position to find a 0 to swap with the 1 on the left side.

### Method 4: Traversing the Array only once: Partition method

In this approach, we use two variables like in the above approach but both from the beginning of the Array. We'll use one variable (i) to traverse the Array to find 0's and the variable (j) to point to the beginning of the Array. If i find a 0, we'll swap the elements at i and j, and then we'll increment j. To clarify, j is the index we want 0's to occupy.

Sample code:

Output:

```Enter the size of the Array: 6
Enter the elements: 1 1 0 0 1 1
The resultant Array: 0 0 1 1 1 1
```

Understanding:

In the above code, we declared a variable j = 0, arr[j] is at the beginning of the Array. Now, we iterate through the Array using i, and in each iteration, we check if arr[i] is 0. If arr[i] is 0, we swap it with arr[j] to send it to the beginning of the Array. Now, we increment the value of j and continue the process.

For example: First iteration -> j = 0, i = 0 -> arr is 1

Second iteration -> j = 0, i = 1 -> arr is 0 and i != j

so swap arr <-> arr j = j + 1

Third iteration -> j = 1, i = 2 -> arr is 1

Fourth iteration -> j = 1, i = 3 -> arr is 0 and i != j

so swap arr <-> arr Fifth iteration -> j = 2, i = 4 -> arr is 0 and i != j

so swap arr <-> arr Sixth iteration -> j = 3, i = 5 -> arr is 0 and i != j so swap arr <-> arr Time complexity: O(n) where n is the size of the Array

### Feedback   