Segregate 0s on Left Side & 1s on Right Side of the Array in Java



Segregation is a process in the field of software engineering where a code is forced to depend on those methods that are not in use. The segregation interface is known as ISP. It splits the interfaces that are vast in nature. In a Java environment, there are lots of advantages to implementing the segregation principle. It increases the readability of the particular code. It also helps to maintain that particular code conveniently.

Problem Statement

There is an array of 7s and 16s aimlessly. We must segregate the 7s on the left side and the 16s on the right. The basic goal is here to traverse those elements using Java.

Input

Input array = [7,16,7,16,7,16,7,16]

Output

Output array = [7,7,7,7,16,16,16,16]   

Different Approaches

Below are the different approaches to segregating 0s on the left side & 1s on the right side of the Array ?

Let's discuss the implementation of the segregation process to perform 0s on the left side & 1s on the right side to an array.

Steps to Segregate 0s on Left Side & 1s on Right Side of the Array:-

  • START.

  • Build up the two different indexes.

  • Declare the first one as 0. (Left)

  • Declare the Second one as n-1.(Right)

  • Follow the condition: left< right.

  • Increment the whole index left( 0s at it)

  • Decrement the whole index right(1s at it)

  • If, left<right then exchange the position.

  • Else, carry forward the process.

Algorithm

int left = 0, right = size-1;
while (left < right){
   while (array[left] == 0 && left < right)
   left++;
   while (array[right] == 1 && left < right)
   right--;
   if (left < right){
      array[left] = 0;
      array[right] = 1;
      left++;
      right--;
   }  
}

In the above algorithm, we can see how the function segregates all the 0s on the left and 1s on the right present in an array.

  • Take two pointers;

    • for element X starting from beginning (index = 0)

    • type1 (for element 1) starting from end (index = array.length-1).

    • Initialize type0 = 0

    • type1 = array.length-1

  • It is necessary to Put 1 on the right side of the array. After the process is done, then 0 will definitely go towards the left.

By using the segregation method by counting

Process: Here is the step-by-step process of the segregation method by counting.

  • Count the 0s number.

  • Traverse the whole array.

  • Search for the elements.

  • Maintain the data and increment it until the result appears.

  • Print.

  • Remaining number of 1s.

  • Print remaining.

Example 

import java.util.*;
import java.util.Arrays;

public class tutorialspoint {
   static void segregate7and16(int arr[], int n){
      int count = 0;
      for (int a = 0; a < n; a++) {
         if (arr[a] == 0)
            count++;
      }

      for (int a = 0; a < count; a++)
      	arr[a] = 0;

      for (int i = count; i < n; i++)
         arr[i] = 1;
      }

      static void print(int arr[], int n){
      System.out.print("Array after segregation list is here ");

      for (int i = 0; i < n; i++)
      	System.out.print(arr[i] + " ");
   }
   public static void main(String[] args){
      int arr[] = new int[] { 7, 16, 7, 16, 16, 16,7,16 };

      int n = arr.length;

      segregate7and16(arr, n);

      print(arr, n);
   }
}

Output

Array after segregation list is here 1 1 1 1 1 1 1 1

Here in this particular code the

Time Complexity: O(n)

Auxiliary Space: O(1)

By using a sorting method on an array

The sort() method belongs to the java.util package :

Syntax

public static void sort(int[] arr, int from_Index, int to_Index)
  • Parameters:

    • arr- array to be sorted

    • from_Index - Inclusive to be sorted.

    • to_Index - exclusive to be sorted.

Example 

import java.util.*;

public class tutoriaspoint {
   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 

Here in this particular code the

Time Complexity: O(nlogn)

Auxiliary Space: O(log n)

By using pointers

It is needed to maintain the left pointer and swap it with the position of the left. When zero is found in the array increment the left pointer.

Example 

import java.util.*;
import java.io.*;

public class tutorialspoint {
   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

Here in this particular code the

Time Complexity: O(n)

Auxiliary Space: O(1)

By using Brute Force approach

Maintain the left pointer and swap it with the position of the left. When zero is found in the array and increment the left pointer.

The method will count the number first. Then it will fill the data accordingly with 0s and 1s.

Process:

  • The process will count the number according to the array size.

  • After counting the filling will start.

Example 

public class segregatearray {
	static void segregating0sand1s(int arr[], int n){
      int count = 0; //count no of zeros in array
      for (int i = 0; i < n; i++) {
      	if (arr[i] == 0)
            count++;
     }
 
     for (int i = 0; i < count; i++)
      	arr[i] = 0; // fill the array with 0 until termination
     for (int i = count; i < n; i++)
      	arr[i] = 1; // fill remaining array with 1 until termination
   }
   static void print(int arr[], int n){
      System.out.print("Array after the process 0s and 1s are ");
      for (int i = 0; i < n; i++)
         System.out.print(arr[i] + " ");
   }
   public static void main(String[] args){
      int arr[] = new int[]{ 1, 1, 0, 0, 1, 1 };
      int n = arr.length;
      segregating0sand1s(arr, n);
      print(arr, n);    
   }
}

Output

Array after the process 0s and 1s are 0 0 1 1 1 1 

Here in this particular code the

Time Complexity: O(n)

Auxiliary Space: O(1)

Conclusion

In this article, we have learned how to segregate all 0s on the left and all 1s on the right as segregate 0 and 1 along with its different approaches, pseudocode, implementation, and code. Here we have defined an array and passed that array along with its total length to the function.

Updated on: 2024-08-06T22:37:14+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements