Java Program to Segregate 0s on Left Side & 1s on Right Side of the Array
Last Updated :
30 Mar, 2022
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
import java.util.*;
import java.util.Arrays;
public class GFG {
static void segregate0and1( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 0 )
count++;
}
for ( int i = 0 ; i < count; i++)
arr[i] = 0 ;
for ( int i = count; i < n; i++)
arr[i] = 1 ;
}
static void print( int arr[], int n)
{
System.out.print( "Array after segregation is " );
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = new int [] { 0 , 1 , 0 , 1 , 1 , 1 };
int n = arr.length;
segregate0and1(arr, n);
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
import java.util.*;
public class GFG {
static void print( int arr[], int n)
{
System.out.print( "Array after segregation is " );
for ( int i = 0 ; i < n; ++i)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = new int [] { 0 , 1 , 0 , 1 , 1 , 1 };
int n = arr.length;
Arrays.sort(arr);
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
import java.util.*;
import java.io.*;
class GFG {
static void print( int a[])
{
System.out.print( "Array after segregation is: " );
for ( int i = 0 ; i < a.length; ++i) {
System.out.print(a[i] + " " );
}
}
public static void main(String[] args)
{
int a[] = { 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 };
int left = 0 ;
for ( int i = 0 ; i < a.length; ++i) {
if (a[i] == 0 ) {
int temp = a[left];
a[left] = a[i];
a[i] = temp;
++left;
}
}
print(a);
}
}
|
Output:
Array after segregation is: 0 0 0 0 1 1 1 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...