Segregate 0s and 1s in an arrayGiven 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 ArrayIf 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(N^{2}). Method 2: Traversing the Array two times: CountingIn 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/ variablesIn 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,
Method 4: Traversing the Array only once: Partition methodIn 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[0] is 1 Second iteration > j = 0, i = 1 > arr[1] is 0 and i != j so swap arr[0] <> arr[1] j = j + 1 Third iteration > j = 1, i = 2 > arr[2] is 1 Fourth iteration > j = 1, i = 3 > arr[3] is 0 and i != j so swap arr[1] <> arr[3] Fifth iteration > j = 2, i = 4 > arr[4] is 0 and i != j so swap arr[2] <> arr[4] Sixth iteration > j = 3, i = 5 > arr[5] is 0 and i != j so swap arr[3] <> arr[5] Time complexity: O(n) where n is the size of the Array
Next TopicArticulation Points and Bridges
