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
// 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
// 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
// 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)
Similar Reads
Java Program to Split an Array from Specified Position In Java, splitting an array means dividing the array into two parts based on a given position. This operation creates two new arrays that represent the segments before and after the given index.Example:The simplest way to split an array in Java from a specified position is by using the in-built Arra
4 min read
Java Program to Split the array and add the first part to the end | Set 2 Given an array and split it from a specified position, and move the first part of array add to the end.  Examples:  Input : arr[] = {12, 10, 5, 6, 52, 36} k = 2 Output : arr[] = {5, 6, 52, 36, 12, 10} Explanation : Split from index 2 and first part {12, 10} add to the end . Input : arr[] = {3, 1,
2 min read
Java Program to Print the Elements of an Array Present on Odd Position An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E
3 min read
Java Program to Move all zeroes to end of array | Set-2 (Using single traversal) Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples:Â Â Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7
2 min read
Java Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0,
6 min read