
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Array of Binary Values in C++
Sorting an array of binary values (0s and 1s) is a common problem in programming. In C++, there are multiple ways to efficiently sort an array containing only 0s and 1s. In this article, we are going to learn various approaches to sorting an array of binary values using C++.
How to Sort an Array of Binary Values?
In this problem, we are given an array that consists of only 0s and 1s. The task is to sort the array in ascending order, which means all 0s should come before all 1s.
Example 1
-
Input:
arr = [1, 0, 1, 0, 1, 0, 0, 1] -
Output:
[0, 0, 0, 0, 1, 1, 1, 1]
Explanation:
All 0s are moved to the beginning and all 1s are moved to the end.
Example 2
-
Input:
arr = [1, 1, 0, 0, 1, 0] -
Output:
[0, 0, 0, 1, 1, 1]
Explanation:
The array is sorted by grouping all 0s before 1s.
Different Approaches to Sorting a Binary Array
Using a Simple Counting Approach
This is the simplest and most direct approach. In this approach, we count the number of 0s in the array and then reconstruct the sorted array. This method is efficient and requires only a single pass to count the binary values (0s and 1s) and another pass to rewrite the values.
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 0, 1, 0, 1, 0, 0, 1}; int n = sizeof(arr) / sizeof(arr[0]); int count0 = 0; for (int i = 0; i < n; i++) { if (arr[i] == 0) count0++; } for (int i = 0; i < count0; i++) arr[i] = 0; for (int i = count0; i < n; i++) arr[i] = 1; cout << "Sorted binary array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
Output:
0 0 0 0 1 1 1 1
Time Complexity: O(N)
Space Complexity: O(1)
Using Two Pointer Approach
In this approach, we use two pointers: one starting from the left (0 index) and the other from the right (n - 1 index). We traverse the array and swap the elements when necessary to ensure all 0s appear before all 1s. This method is efficient as it sorts the array in place without requiring extra space.
Implementation Code:
#include<bits/stdc++.h> using namespace std; void sortBinaryArray(int arr[], int n) { int left = 0, right = n - 1; while (left < right) { while (arr[left] == 0 && left < right) left++; while (arr[right] == 1 && left < right) right--; if (left < right) { swap(arr[left], arr[right]); left++; right--; } } } int main() { int arr[] = {1, 1, 0, 0, 1, 0}; int n = sizeof(arr) / sizeof(arr[0]); sortBinaryArray(arr, n); cout << "Sorted binary array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
Output:
0 0 0 1 1 1
Time Complexity: O(N)
Space Complexity: O(1)
Using the STL sort() Function
A straightforward and simple approach is to use the in-built sorting function provided by C++ STL. This sort() function is highly optimized and can handle large arrays efficiently. However, it has a higher time complexity than the previous methods.
Implementation Code:
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 0, 1, 0, 1, 0, 0, 1}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n); cout << "Sorted binary array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
Output:
0 0 0 0 1 1 1 1
Time Complexity: O(N log N)
Space Complexity: O(1)
Using the Stable Partition Algorithm
The stable_partition() function from the library in C++ is one of the efficient methods to sort a binary array. This function groups elements based on a condition while maintaining their relative order.
Implementation Code:
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 0, 1, 0, 1, 0, 0, 1}; int n = sizeof(arr) / sizeof(arr[0]); stable_partition(arr, arr + n, [](int x) { return x == 0; }); cout << "Sorted binary array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; }
Output:
0 0 0 0 1 1 1 1
Time Complexity: O(N)
Space Complexity: O(1)