Sort an array containing two types of elements - GeeksforGeeks

06 Apr, 2021
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 arrayIn 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 1) 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 2) else it will be 0 then we need to simple increment index type0
C++14
Java
Python3
C#
PHP
Javascript
// CPP program to sort an array with two types// of values in one traversal.#include <bits/stdc++.h>using namespace std; /* Method for segregation 0 and 1 given   input array */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++;        }    }} // Driver programint 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 program to sort an array with two types// of values in one traversal.public class GFG {/* Method for segregation 0 and 1     given input array */class segregation {    static void segregate0and1(int arr[], int n)    {        int type0 = 0;        int type1 = n - 1;         while (type0 < type1) {            if (arr[type0] == 1) {                 // swap type0 and type1                arr[type0] = arr[type0] + arr[type1];                arr[type1] = arr[type0] - arr[type1];                arr[type0] = arr[type0] - arr[type1];                type1--;            }            else {                type0++;            }        }    }     // Driver program    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 program to sort an array with# two types of values in one traversal. # Method for segregation 0 and# 1 given input arraydef 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         # Driver Codearr = [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 = " ") # This code is contributed by Smitha Dinesh Semwal
// C# program to sort an array with two types// of values in one traversal.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)            {                 // swap type0 and type1                arr[type0] = arr[type0] + arr[type1];                arr[type1] = arr[type0]-arr[type1];                arr[type0] = arr[type0]-arr[type1];                type1--;            }            else {                type0++;            }        }    }     // Driver program    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] + " ");    }} // This code is contributed by vt_m.
<?php// PHP program to sort an array with two// types of values in one traversal. /* Method for segregation 0 and 1given input array */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;} // Driver Code$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] . " ";         // This code is contributed by Rajput-Ji?>
<script> // JavaScript program to sort an array with two types// of values in one traversal.     function segregate0and1(arr, n)    {        let type0 = 0;        let type1 = n - 1;          while (type0 < type1)        {                          if (arr[type0] == 1)            {                  // swap type0 and type1                arr[type0] = arr[type0] + arr[type1];                arr[type1] = arr[type0]-arr[type1];                arr[type0] = arr[type0]-arr[type1];                type1--;            }            else {                type0++;            }        }    } // Driver Code     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] + " "); // This code is contributed by avijitmondal1998.</script>
0 0 0 1 1 1 1 1 1 1 1
 
Time Complexity : O(n) 
Auxiliary Space: O(1)
san4net
Rajput-Ji
VishalBachchas
unholycat
subhammahato348
avijitmondal1998
limited-range-elements
Arrays
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction to Arrays
Multidimensional Arrays in Java
Queue | Set 1 (Introduction and Array Implementation)
Linked List vs Array
Python | Using 2D arrays/lists the right way