You are given an array of 0s and 1s in random order. Segregate 0s on the left side and 1s on the right side of the array. The basic goal is to traverse array elements and sort in segregating 0s and 1s.
Illustration:
Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
Approaches:
- Using segregation by counting
- Using sorting of an array
- Using pointers
Below all three approaches all discussed in detail:
Approach 1:
- Count the number of 0s.
- Traversing over the whole array for looking out indices where zeros are present
- Maintaining a count and incrementing as 0 appears
- Print all zeros to the front
- The remaining number of 1s will be 1- (total number of 0s)
- Print the remaining elements
Below is the implementation to segregate 0s and 1s using the above algorithm:
// Java code to Segregate 0s and 1s in an array // Importing generic libraries import java.util.*;
// Importing Array libraries import java.util.Arrays;
public class GFG {
// Function to segregate 0s and 1s
static void segregate0and1( int arr[], int n)
{
// Counts the no of zeros in array
int count = 0 ;
// Iteration over array
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 0 )
// Incrementing the count
count++;
}
// Loop fills the arr with 0 until count
for ( int i = 0 ; i < count; i++)
arr[i] = 0 ;
// Loop fills remaining arr space with 1
for ( int i = count; i < n; i++)
arr[i] = 1 ;
}
// Function to print segregated array
static void print( int arr[], int n)
{
System.out.print( "Array after segregation is " );
// Iteration over array
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
// Main driver function
public static void main(String[] args)
{
// Array taken for consideration
int arr[] = new int [] { 0 , 1 , 0 , 1 , 1 , 1 };
// Using inbuilt function to store array size
int n = arr.length;
// Calling function that segregates array
segregate0and1(arr, n);
// Printing the above segregated array
print(arr, n);
}
} |
Output:
Array after segregation is 0 0 1 1 1 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach 2: Using sort() function
sort() Is a method is a java.util.Arrays class method.
Syntax:
public static void sort(int[] arr, int from_Index, int to_Index)
Parameters:
arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted
Return Type:
This method doesn't return any value
// Java code to Segregate 0s and 1s in an array // Importing generic libraries import java.util.*;
public class GFG {
// Function to print segregated array
// Taking arguments- array and array size
static void print( int arr[], int n)
{
System.out.print( "Array after segregation is " );
// Iteration over array
for ( int i = 0 ; i < n; ++i)
// Printing array elements
System.out.print(arr[i] + " " );
}
// Main driver function
public static void main(String[] args)
{
// Array taken for consideration
int arr[] = new int [] { 0 , 1 , 0 , 1 , 1 , 1 };
// Using length inbuilt function to
int n = arr.length;
// Using sort inbuilt function
Arrays.sort(arr);
// Printing elements after executing sorting
print(arr, n);
}
} |
Output:
Array after segregation is 0 0 1 1 1 1
Time Complexity: O(n*logn)
Auxiliary Space: O(1)
Approach 3: Maintain the left pointer and swap with the position of the left when zero found in the array and increment the left pointer.
// Java code to Segregate 0s and 1s in an array // Importing generic libraries import java.util.*;
import java.io.*;
class GFG {
// Print function outside main to print elements
static void print( int a[])
{
System.out.print( "Array after segregation is: " );
// Iteration over array using array
// class inbuilt function .length()
for ( int i = 0 ; i < a.length; ++i) {
// Printing elements in array
System.out.print(a[i] + " " );
}
}
// Main driver method
public static void main(String[] args)
{
// Random array taken for consideration
int a[] = { 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 };
// Maintaining left pointer
int left = 0 ;
// Iteration over array using length function
for ( int i = 0 ; i < a.length; ++i) {
// If zeros are present
if (a[i] == 0 ) {
// Swap the elements using
// temporary variable
int temp = a[left];
a[left] = a[i];
a[i] = temp;
// Pre incrementing left pointer
++left;
}
}
// Calling above function to
// print updated array
print(a);
}
} |
Output:
Array after segregation is: 0 0 0 0 1 1 1 1
Time Complexity: O(n)
Auxiliary Space: O(1)