Sort a binary array using one traversal

Given a binary array, sort it using one traversal and no extra space.

Examples :

Input : 1 0 0 1 0 1 0 1 1 1 1 1 1 0 0 1 1 0 1 0 0 
Output : 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1

Input : 1 0 1 0 1 0 1 0 
Output : 0 0 0 0 1 1 1 1

This concept is related to partition of quick sort . In quick sort’ partition, after one scan, the left of array in smallest and right of array is largest of selected pivot element. So this concept related to quick sort.but it works in O(n) time only.

let’s understand this concept with this code

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to sort a binary array
#include <iostream>
using namespace std;
  
void sortBinaryArray(int a[], int n)
{
    int j = -1;
    for (int i = 0; i < n; i++) {
  
        // if number is smaller than 1
        // then swap it with j-th number
        if (a[i] < 1) {
            j++;
            swap(a[i], a[j]);
        }
    }
}
  
// Driver code
int main()
{
    int a[] = { 1, 0, 0, 1, 0, 1, 0, 1, 1, 1,
                1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0 };
    int n = sizeof(a) / sizeof(a[0]);
    sortBinaryArray(a, n);
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Code for Sort a binary
// array using one traversal
import java.util.*;
  
class GFG {
  
    static void sortBinaryArray(int a[], int n)
    {
        int j = -1;
        for (int i = 0; i < n; i++) {
  
            // if number is smaller than 1
            // then swap it with j-th number
            if (a[i] < 1) {
                j++;
                int temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }
  
    /* Driver program to test above function */
    public static void main(String[] args)
    {
  
        int a[] = { 1, 0, 0, 1, 0, 1, 0, 1, 1, 1,
                    1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0 };
  
        int n = a.length;
  
        sortBinaryArray(a, n);
  
        for (int i = 0; i < n; i++)
            System.out.print(a[i] + " ");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A Python program to sort a
# binary array
def sortBinaryArray(a, n):
    j = -1
    for i in range(n):
  
        # if number is smaller
        # than 1 then swap it
        # with j-th number
        if a[i] < 1:
            j = j + 1
              
            # swap
            a[i], a[j] = a[j], a[i]
      
  
# Driver program
a = [1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1,
        1, 1, 0, 0, 1, 1, 0, 1, 0, 0]
n = len(a)
  
sortBinaryArray(a, n)
  
for i in range(n):
        print(a[i], end = " ")
  
# This code is contributed by Shrikant13.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code for Sort a binary
// array using one traversal
using System;
  
class GFG {
  
    static void sortBinaryArray(int[] a,
                                  int n)
    {
        int j = -1;
        for (int i = 0; i < n; i++)
        {
  
            // if number is smaller than
            // 1 then swap it with j-th 
            // number
            if (a[i] < 1) {
                j++;
                int temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }
  
    /* Driver program to test above
    function */
    public static void Main()
    {
  
        int[] a = { 1, 0, 0, 1, 0, 1, 0,
                    1, 1, 1, 1, 1, 1, 0,
                    0, 1, 1, 0, 1, 0, 0 };
  
        int n = a.Length;
  
        sortBinaryArray(a, n);
  
        for (int i = 0; i < n; i++)
            Console.Write(a[i] + " ");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Code for Sort a binary
// array using one traversal
function sortBinaryArray($a, $n)
{
    $j = -1;
    for ($i = 0; $i < $n; $i++)
    {
  
        // if number is smaller than
        // 1 then swap it with j-th 
        // number
        if ($a[$i] < 1) 
        {
            $j++;
            $temp = $a[$j];
            $a[$j] = $a[$i];
            $a[$i] = $temp;
        }
    }
for ($i = 0; $i < $n; $i++)
        echo $a[$i] . " ";
      
}
  
  
// Driver Code
$a = array(1, 0, 0, 1, 0, 1, 0,
           1, 1, 1, 1, 1, 1, 0,
           0, 1, 1, 0, 1, 0, 0);
  
$n = count($a);
sortBinaryArray($a, $n);
  
// This code is contributed by Sam007
?>

chevron_right



Output :

0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1

Time Complexity : O(n).


This article is contributed by Devanshu Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, shrikanth13, Sam007



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.