Sort an array containing two types of elements
Given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.
Examples:
Input : arr[] = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
Output : arr[] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
Input : arr[] = [1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1]
Output : arr[] = [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
We have already discussed a solution Segregate 0s and 1s in an array
In this post, a new solution is discussed.
- Step 1 : Here we can take two pointers type0 (for element 0) starting from beginning (index = 0) and type1 (for element 1) starting from end index.
- Step 2: We intend to put 1 to the right side of the array. Once we have done this then 0 will definitely towards left side of array to achieve this we do following.
We compare elements at index type0
- if this is 1 then this should be moved to right side so we need to swap this with index type1 once swapped we are sure that element at index type1 is ‘1’ so we need to decrement index type1
- else it will be 0 then we need to simple increment index type0
Implementation:
C++14
#include <bits/stdc++.h>
using namespace std;
void segregate0and1( int arr[], int n)
{
int type0 = 0;
int type1 = n - 1;
while (type0 < type1) {
if (arr[type0] == 1) {
swap(arr[type0], arr[type1]);
type1--;
}
else {
type0++;
}
}
}
int main()
{
int arr[] = { 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1 };
int n = sizeof (arr)/ sizeof (arr[0]);
segregate0and1(arr, n);
for ( int a : arr)
cout << a << " " ;
}
|
Java
class segregation {
static void segregate0and1( int arr[], int n)
{
int type0 = 0 ;
int type1 = n - 1 ;
while (type0 < type1) {
if (arr[type0] == 1 ) {
arr[type0] = arr[type0] + arr[type1];
arr[type1] = arr[type0] - arr[type1];
arr[type0] = arr[type0] - arr[type1];
type1--;
}
else {
type0++;
}
}
}
public static void main(String[] args)
{
int arr[]
= { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 };
segregate0and1(arr, arr.length);
for ( int a : arr)
System.out.print(a + " " );
}
}
|
Python3
def segregate0and1(arr, n):
type0 = 0 ; type1 = n - 1
while (type0 < type1):
if (arr[type0] = = 1 ):
arr[type0], arr[type1] = arr[type1], arr[type0]
type1 - = 1
else :
type0 + = 1
arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ]
n = len (arr)
segregate0and1(arr, n)
for i in range ( 0 , n):
print (arr[i], end = " " )
|
C#
using System;
class GFG {
static void segregate0and1( int []arr, int n)
{
int type0 = 0;
int type1 = n - 1;
while (type0 < type1)
{
if (arr[type0] == 1)
{
arr[type0] = arr[type0] + arr[type1];
arr[type1] = arr[type0]-arr[type1];
arr[type0] = arr[type0]-arr[type1];
type1--;
}
else {
type0++;
}
}
}
public static void Main()
{
int []arr = { 1, 1, 1, 0, 1, 0, 0,
1, 1, 1, 1 };
segregate0and1(arr, arr.Length);
for ( int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " " );
}
}
|
PHP
<?php
function segregate0and1( $arr , $n )
{
$type0 = 0;
$type1 = $n - 1;
while ( $type0 < $type1 )
{
if ( $arr [ $type0 ] == 1)
{
$temp = $arr [ $type0 ];
$arr [ $type0 ] = $arr [ $type1 ];
$arr [ $type1 ] = $temp ;
$type1 --;
}
else
{
$type0 ++;
}
}
return $arr ;
}
$arr = array ( 1, 1, 1, 0, 1, 0,
0, 1, 1, 1, 1 );
$n = count ( $arr );
$arr1 = segregate0and1( $arr , $n );
for ( $i = 0; $i < $n ; $i ++ )
echo $arr1 [ $i ] . " " ;
?>
|
Javascript
<script>
function segregate0and1(arr, n)
{
let type0 = 0;
let type1 = n - 1;
while (type0 < type1)
{
if (arr[type0] == 1)
{
arr[type0] = arr[type0] + arr[type1];
arr[type1] = arr[type0]-arr[type1];
arr[type0] = arr[type0]-arr[type1];
type1--;
}
else {
type0++;
}
}
}
let arr = [1, 1, 1, 0, 1, 0, 0,
1, 1, 1, 1 ];
segregate0and1(arr, arr.length);
for (let i = 0; i < arr.length; i++)
document.write(arr[i] + " " );
</script>
|
Output:
0 0 0 1 1 1 1 1 1 1 1
Time Complexity : O(n)
Auxiliary Space: O(1)
Approach 2:- Using counting sort
Another approach to sort an array containing two types of elements is to use counting sort. Here’s the basic idea:
- Traverse the array and count the number of occurrences of each type of element.
- Create a new array of the same size as the original array.
- Copy the first type of elements into the new array, starting from index 0.
- Copy the second type of elements into the new array, starting from the end of the array.
- The new array is now sorted.
C++
#include <bits/stdc++.h>
using namespace std;
void sortTwoTypes( int arr[], int n)
{
int count[2]
= { 0 };
for ( int i = 0; i < n; i++)
count[arr[i]]++;
int i = 0;
for ( int j = 0; j < 2; j++) {
while (count[j]--)
arr[i++] = j;
}
}
int main()
{
int arr[] = { 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
sortTwoTypes(arr, n);
for ( int a : arr)
cout << a << " " ;
}
|
Java
import java.util.*;
public class Main {
static void sortTwoTypes( int [] arr, int n)
{
int [] count
= { 0 ,
0 };
for ( int i = 0 ; i < n; i++)
count[arr[i]]++;
int i = 0 ;
for ( int j = 0 ; j < 2 ; j++) {
while (count[j]-- > 0 )
arr[i++] = j;
}
}
public static void main(String[] args)
{
int [] arr = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 };
int n = arr.length;
sortTwoTypes(arr, n);
for ( int a : arr)
System.out.print(a + " " );
}
}
|
Python3
def sortTwoTypes(arr, n):
count = [ 0 , 0 ]
for i in range (n):
count[arr[i]] + = 1
i = 0
for j in range ( 2 ):
while count[j] > 0 :
arr[i] = j
i + = 1
count[j] - = 1
arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ]
n = len (arr)
sortTwoTypes(arr, n)
for a in arr:
print (a, end = " " )
|
C#
using System;
public class MainClass
{
static void SortTwoTypes( int [] arr, int n)
{
int [] count = { 0, 0 };
for ( int i = 0; i < n; i++)
count[arr[i]]++;
int k = 0;
for ( int j = 0; j < 2; j++)
{
while (count[j]-- > 0)
arr[k++] = j;
}
}
public static void Main( string [] args)
{
int [] arr = { 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1 };
int n = arr.Length;
SortTwoTypes(arr, n);
foreach ( int a in arr)
Console.Write(a + " " );
}
}
|
Javascript
function sortTwoTypes(arr, n) {
let count = [0, 0];
for (let i = 0; i < n; i++) {
count[arr[i]] += 1;
}
let i = 0;
for (let j = 0; j < 2; j++) {
while (count[j] > 0) {
arr[i] = j;
i += 1;
count[j] -= 1;
}
}
}
let arr = [1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1];
let n = arr.length;
sortTwoTypes(arr, n); let temp = "" ;
for (let a of arr) {
temp = temp + a + " " ;
} console.log(temp);
|
Output
0 0 0 1 1 1 1 1 1 1 1
Time Complexity: O(n), where n is the size of the array
Auxiliary Space: O(1)
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...