forked from ArmanKumar21/python-cpp-html-programs-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort012.cpp
More file actions
45 lines (42 loc) · 721 Bytes
/
sort012.cpp
File metadata and controls
45 lines (42 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
void sort012(int arr[],int n)
{
int o=0,t=0,z=0;
for(int i=0;i<n;i++) // Counting the number of zeroes, ones and twos
{
if(arr[i]==0){
z++;
}
else if(arr[i]==1)
{
o++;
}
else{
t++;
}
}
for(int i=0;i<z;i++) // Updating their positions in array
{
arr[i]=0;
}
for(int i=z;i<(z+o);i++)
{
arr[i]=1;
}
for(int i=z+o;i<n;i++)
{
arr[i]=2;
}
}
int main()
{
//To check
// int a[7]={0,1,0,2,1,0,1};
// sort012(a,7);
// for(auto x:a)
// {
// cout<<x<<" ";
// }
return 0;
}