You might be given an array of 0s and 1s in random order. Segregate 0s on left aspect and 1s on proper aspect of the array [Basically you have to sort the array]. Traverse array solely as soon as.
Methodology 1 (Rely 0s or 1s) Due to Naveen for suggesting this methodology. 1) Rely the variety of 0s. So let’s perceive with an instance we have now an array arr = [0, 1, 0, 1, 0, 0, 1] the dimensions of the array is 7 now we’ll traverse your complete array and discover out the variety of zeros within the array, On this case the variety of zeros is 4 so now we will simply get the variety of Ones within the array by Array Size – Quantity Of Zeros.
2) As soon as we have now counted, we will fill the array first we’ll put the zeros after which ones (we will get variety of ones through the use of above method).
C++
#embrace <bits/stdc++.h>
utilizingnamespacestd;
voidsegregate0and1(intarr[], intn)
{
intrely = 0;
for(inti = 0; i < n; i++) {
if(arr[i] == 0)
rely++;
}
for(inti = 0; i < rely; i++)
arr[i] = 0;
for(inti = rely; i < n; i++)
arr[i] = 1;
}
voidprint(intarr[], intn)
{
cout << "Array after segregation is ";
for(inti = 0; i < n; i++)
cout << arr[i] << " ";
}
intmost important()
{
intarr[] = { 0, 1, 0, 1, 1, 1 };
intn = sizeof(arr) / sizeof(arr[0]);
segregate0and1(arr, n);
print(arr, n);
return0;
}
Java
classGFG {
staticvoidsegregate0and1(intarr[], intn)
{
intrely = 0;
for(inti = 0; i < n; i++) {
if(arr[i] == 0)
rely++;
}
for(inti = 0; i < rely; i++)
arr[i] = 0;
for(inti = rely; i < n; i++)
arr[i] = 1;
}
staticvoidprint(intarr[], intn)
{
System.out.print("Array after segregation is ");
for(inti = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
publicstaticvoidmost important(String[] args)
{
intarr[] = newint[]{ 0, 1, 0, 1, 1, 1};
intn = arr.size;
segregate0and1(arr, n);
print(arr, n);
}
}
Python3
defsegregate0and1(arr, n) :
rely =0
fori invary(0, n) :
if(arr[i] ==0) :
rely =rely +1
fori invary(0, rely) :
arr[i] =0
fori invary(rely, n) :
arr[i] =1
defprint_arr(arr , n) :
print( "Array after segregation is ",finish ="")
fori invary(0, n) :
print(arr[i] , finish =" ")
arr =[ 0, 1, 0, 1, 1, 1]
n =len(arr)
segregate0and1(arr, n)
print_arr(arr, n)
C#
utilizingSystem;
classGFG {
staticvoidsegregate0and1(int[]arr, intn)
{
intrely = 0;
for(inti = 0; i < n; i++) {
if(arr[i] == 0)
rely++;
}
for(inti = 0; i < rely; i++)
arr[i] = 0;
for(inti = rely; i < n; i++)
arr[i] = 1;
}
staticvoidprint(int[]arr, intn)
{
Console.WriteLine("Array after segregation is ");
for(inti = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
publicstaticvoidFundamental()
{
int[]arr = newint[]{ 0, 1, 0, 1, 1, 1 };
intn = arr.Size;
segregate0and1(arr, n);
print(arr, n);
}
}
PHP
<?php
operatesegregate0and1(&$arr, $n)
{
$rely= 0;
for($i= 0; $i< $n; $i++)
{
if($arr[$i] == 0)
$rely++;
}
for($i= 0; $i< $rely; $i++)
$arr[$i] = 0;
for($i= $rely; $i< $n; $i++)
$arr[$i] = 1;
}
operatetoprint(&$arr, $n)
{
echo("Array after segregation is ");
for($i= 0; $i< $n; $i++)
echo( $arr[$i] . " ");
}
$arr= array(0, 1, 0, 1, 1, 1 );
$n= sizeof($arr);
segregate0and1($arr, $n);
toprint($arr, $n);
?>
Javascript
<script>
operatesegregate0and1(arr, n)
{
let rely = 0;
for(let i = 0; i < n; i++) {
if(arr[i] == 0)
rely++;
}
for(let i = 0; i < rely; i++)
arr[i] = 0;
for(let i = rely; i < n; i++)
arr[i] = 1;
}
operateprint(arr, n)
{
doc.write("Array after segregation is ");
for(let i = 0; i < n; i++)
doc.write(arr[i] + " ");
}
let arr = [ 0, 1, 0, 1, 1, 1 ];
let n = arr.size;
segregate0and1(arr, n);
print(arr, n);
</script>
Output
Array after segregation is 0 0 1 1 1 1
Time Complexity : O(2n) ≅ O(n) Auxiliary Area: O(1)
Methodology 1 traverses the array two occasions. Methodology 2 does the identical in a single cross.
Methodology 1 has time complexity of O(2n) and Methodology 2 has time complexity of O(n)
Methodology 2 (Use two indexes to traverse) Keep two indexes. Initialize the primary index left as 0 and second index proper as n-1. Do following whereas left < proper a) Preserve incrementing index left whereas there are 0s at it b) Preserve decrementing index proper whereas there are 1s at it c) If left < proper then change arr[left] and arr[right]
Implementation:
C++
#embrace <bits/stdc++.h>
utilizingnamespacestd;
voidsegregate0and1(intarr[], intmeasurement)
{
intleft = 0, proper = size-1;
whereas(left < proper)
{
whereas(arr[left] == 0 && left < proper)
left++;
whereas(arr[right] == 1 && left < proper)
right--;
if(left < proper)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}
intmost important()
{
intarr[] = {0, 1, 0, 1, 1, 1};
inti, arr_size = sizeof(arr)/sizeof(arr[0]);
segregate0and1(arr, arr_size);
cout << "Array after segregation ";
for(i = 0; i < 6; i++)
cout << arr[i] << " ";
return0;
}
C
#embrace<stdio.h>
voidsegregate0and1(intarr[], intmeasurement)
{
intleft = 0, proper = size-1;
whereas(left < proper)
{
whereas(arr[left] == 0 && left < proper)
left++;
whereas(arr[right] == 1 && left < proper)
right--;
if(left < proper)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}
intmost important()
{
intarr[] = {0, 1, 0, 1, 1, 1};
inti, arr_size = sizeof(arr)/sizeof(arr[0]);
segregate0and1(arr, arr_size);
printf("Array after segregation ");
for(i = 0; i < 6; i++)
printf("%d ", arr[i]);
getchar();
return0;
}
Java
classSegregate
{
voidsegregate0and1(intarr[], intmeasurement)
{
intleft = 0, proper = measurement - 1;
whereas(left < proper)
{
whereas(arr[left] == 0&& left < proper)
left++;
whereas(arr[right] == 1&& left < proper)
right--;
if(left < proper)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}
publicstaticvoidmost important(String[] args)
{
Segregate seg = newSegregate();
intarr[] = newint[]{0, 1, 0, 1, 1, 1};
inti, arr_size = arr.size;
seg.segregate0and1(arr, arr_size);
System.out.print("Array after segregation is ");
for(i = 0; i < 6; i++)
System.out.print(arr[i] + " ");
}
}
Python
defsegregate0and1(arr, measurement):
left, proper =0, measurement-1
whereasleft < proper:
whereasarr[left] ==0andleft < proper:
left +=1
whereasarr[right] ==1andleft < proper:
proper -=1
ifleft < proper:
arr[left] =0
arr[right] =1
left +=1
proper -=1
returnarr
arr =[0, 1, 0, 1, 1, 1]
arr_size =len(arr)
print("Array after segregation")
print(segregate0and1(arr, arr_size))
C#
utilizingSystem;
classSegregate
{
voidsegregate0and1(int[]arr, intmeasurement)
{
intleft = 0, proper = measurement - 1;
whereas(left < proper)
{
whereas(arr[left] == 0 && left < proper)
left++;
whereas(arr[right] == 1 && left < proper)
right--;
if(left < proper)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}
publicstaticvoidFundamental()
{
Segregate seg = newSegregate();
int[]arr = newint[]{0, 1, 0, 1, 1, 1};
inti, arr_size = arr.Size;
seg.segregate0and1(arr, arr_size);
Console.WriteLine("Array after segregation is ");
for(i = 0; i < 6; i++)
Console.Write(arr[i] + " ");
}
}
PHP
<?php
operatesegregate0and1(&$arr, $measurement)
{
$left= 0;
$proper= $measurement- 1;
whereas($left< $proper)
{
whereas($arr[$left] == 0 &&
$left< $proper)
$left++;
whereas($arr[$right] == 1 &&
$left< $proper)
$proper--;
if($left< $proper)
{
$arr[$left] = 0;
$arr[$right] = 1;
$left++;
$proper--;
}
}
}
$arr= array(0, 1, 0, 1, 1, 1);
$arr_size= sizeof($arr);
segregate0and1($arr, $arr_size);
printf("Array after segregation is ");
for($i= 0; $i< 6; $i++)
echo($arr[$i]. " ");
?>
Javascript
<script>
operatesegregate0and1(arr, measurement)
{
let left = 0, proper = size-1;
whereas(left < proper)
{
whereas(arr[left] == 0 && left < proper)
left++;
whereas(arr[right] == 1 && left < proper)
right--;
if(left < proper)
{
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}
let arr = [0, 1, 0, 1, 1, 1];
let i, arr_size = arr.size;
segregate0and1(arr, arr_size);
doc.write("Array after segregation ");
for(i = 0; i < 6; i++)
doc.write(arr[i] + " ");
</script>
Output
Array after segregation 0 0 1 1 1 1
Time Complexity : O(n) Auxiliary Area: O(1)
One other method : 1. Take two pointer type0(for aspect 0) ranging from starting (index = 0) and type1(for aspect 1) ranging from finish (index = array.length-1). Initialize type0 = 0 and type1 = array.length-1 2. It’s meant to Put 1 to the precise aspect of the array. As soon as it’s performed, then 0 will certainly in direction of the left aspect of the array.
C++
#embrace <bits/stdc++.h>
utilizingnamespacestd;
voidsegregate0and1(intarr[], intmeasurement)
{
inttype0 = 0;
inttype1 = measurement - 1;
whereas(type0 < type1) {
if(arr[type0] == 1) {
if(arr[type1] != 1) {
swap(arr[type0], arr[type1]);
}
type1--;
}
else
type0++;
}
}
intmost important()
{
intarr[] = { 0, 1, 0, 1, 1, 1 };
inti, arr_size = sizeof(arr) / sizeof(arr[0]);
segregate0and1(arr, arr_size);
cout << "Array after segregation is ";
for(i = 0; i < arr_size; i++)
cout << arr[i] << " ";
return0;
}
Java
importjava.util.*;
classGFG {
/**
Methodology for segregation 0 and 1 given enter array
*/
staticvoidsegregate0and1(intarr[])
{
inttype0 = 0;
inttype1 = arr.size - 1;
whereas(type0 < type1) {
if(arr[type0] == 1) {
if(arr[type1] != 1) {
arr[type1] = arr[type1] + arr[type0];
arr[type0] = arr[type1] - arr[type0];
arr[type1] = arr[type1] - arr[type0];
}
type1--;
}
else{
type0++;
}
}
}
publicstaticvoidmost important(String[] args)
{
int[] array = { 0, 1, 0, 1, 1, 1};
segregate0and1(array);
for(inta : array) {
System.out.print(a + " ");
}
}
}
Python3
defsegregate0and1(arr, measurement):
type0 =0
type1 =measurement -1
whereas(type0 < type1):
if(arr[type0] ==1):
if(arr[type1] !=1):
(arr[type0],
arr[type1]) =(arr[type1],
arr[type0])
type1 -=1
else:
type0 +=1
arr =[0, 1, 0, 1, 1, 1]
arr_size =len(arr)
segregate0and1(arr, arr_size)
print("Array after segregation is",
finish =" ")
fori invary(0, arr_size):
print(arr[i], finish =" ")
C#
utilizingSystem;
classGFG {
staticvoidsegregate0and1(int[] arr)
{
inttype0 = 0;
inttype1 = arr.Size - 1;
whereas(type0 < type1) {
if(arr[type0] == 1) {
if(arr[type1] != 1) {
arr[type1] = arr[type1] + arr[type0];
arr[type0] = arr[type1] - arr[type0];
arr[type1] = arr[type1] - arr[type0];
}
type1--;
}
else{
type0++;
}
}
}
publicstaticvoidFundamental(string[] args)
{
int[] array = newint[] { 0, 1, 0, 1, 1, 1 };
segregate0and1(array);
Console.Write("Array after segregation is ");
foreach(inta inarray) { Console.Write(a + " "); }
}
}
PHP
<?php
operatesegregate0and1(&$arr, $measurement)
{
$type0= 0;
$type1= $measurement- 1;
whereas($type0< $type1)
{
if($arr[$type0] == 1)
{
if($arr[$type1] != 1)
{
$temp= $arr[$type0];
$arr[$type0] = $arr[$type1];
$arr[$type1] = $temp;
}
$type1--;
}
else
$type0++;
}
}
$arr= array(0, 1, 0, 1, 1, 1);
$arr_size= sizeof($arr);
segregate0and1($arr, $arr_size);
echo("Array after segregation is ");
for($i= 0; $i< $arr_size; $i++)
echo($arr[$i] . " ");
?>
Javascript
<script>
operatesegregate0and1(arr, measurement)
{
let type0 = 0;
let type1 = measurement - 1;
whereas(type0 < type1)
{
if(arr[type0] == 1)
{
if(arr[type1] != 1)
{
arr[type1] = arr[type1] + arr[type0];
arr[type0] = arr[type1] - arr[type0];
arr[type1] = arr[type1] - arr[type0];
}
type1--;
}
else
type0++;
}
}
let arr = [ 0, 1, 0, 1, 1, 1 ];
let i, arr_size = arr.size;
segregate0and1(arr, arr_size);
doc.write("Array after segregation is ");
for(i = 0; i < arr_size; i++)
doc.write(arr[i] + " ");
</script>
Output
Array after segregation is 0 0 1 1 1 1
Time complexity: O(n)
Auxiliary Area: O(1) // Thanks san4net for suggesting this methodology.
Please write feedback should you discover any of the above algorithms/code incorrect, or a greater approach to clear up the identical downside.