
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sort an arrays of 0’s, 1’s and 2’s using C++
Given an array of 0, 1, and 2, sort the elements in an order such that all the zeros come first before 1 and all the 2’s in the end. We have to sort all the elements of the array in-place.
We can solve this problem using DNF (Dutch National Flag) Sorting Algorithm. For example,
Input-1 −
arr[ ]= {2,0,0,1,2,1 }
Output −
0 0 1 1 2 2
Explanation − Sorting the given array of elements containing 0,1 and 2 using DNF Sorting Algorithm, it will print the output as {0,0,1,1,2,2}.
Input-2 −
arr[ ]= {0,1,1,2,1,1,0}
Output −
0 0 1 1 1 1 2
Explanation − Sorting the given array of elements containing 0,1 and 2 using the DNF Sorting Algorithm, it will print the output as {0,0,1,1,1,1,2}.
Approach to solve this Problem
In the given array of 0, 1 and 2, we can use the DNF sorting algorithm.
DNF Sorting Algorithm − The algorithm requires 3 pointers to iterate throughout the array by swapping the necessary elements.
Create a low pointer at the beginning of the array and high pointer pointing at the end of the array.
Find the Midpoint of the array and create a mid pointer as well that iterates from the beginning of the array till the end.
If the mid-pointer of the array is ‘0’, then swap the element pointing at low. Increment the low pointer and mid pointer.
If the mid-pointer of the array is ‘2’, then swap it with the element pointing at the high. Increment the mid pointer and decrement the high pointer.
If the mid-pointer of the array is ‘1’, then increase the mid pointer.
Example
#include<iostream> using namespace std; void dnfsort(int a[], int n){ int low= 0; int high= n-1; int mid=0; while(mid<=high){ if(a[mid]==0){ swap(a[mid],a[low]); mid++; low++; } if(a[mid]==1){ mid++; } if(a[mid]==2){ swap(a[mid],a[high]); high--; } } } int main(){ int a[]= {1,0,0,2,1,1,0,0,1}; int n= sizeof(a)/sizeof(int); dnfsort(a,n); for(int i=0;i<n;i++){ cout<<a[i]<<" "; } return 0; }
Output
Running the above code will generate the output as,
0 0 0 0 1 1 1 1 2
- Related Articles
- Sort an arrays of 0’s, 1’s and 2’s using Java
- Segregate 0’s and 1’s in an array list using Python?
- Construct DFA of alternate 0’s and 1’s
- Find the Pattern of 1’s inside 0’s using C++
- Maximum length of segments of 0’s and 1’s in C++
- 1’s and 2’s complement of a Binary Number?
- Count subarrays with equal number of 1’s and 0’s in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- C++ Largest Subtree having Equal No of 1's and 0's
- 1's Complement vs 2's Complement
- An object travels $20\ m$ in $5\ s$ and then another $40\ m$ in $5\ s$. What is the average speed of the object?$6\ m/s$$0\ m/s$$12\ m/s$$2\ m/s$
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
