-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortarray.cpp
60 lines (45 loc) · 742 Bytes
/
sortarray.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
void sortArr(int arr[], int n)
{
int i, cnt0 = 0, cnt1 = 0, cnt2 = 0;
for (i = 0; i < n; i++) {
switch (arr[i]) {
case 0:
cnt0++;
break;
case 1:
cnt1++;
break;
case 2:
cnt2++;
break;
}
}
i = 0;
while (cnt0 > 0) {
arr[i++] = 0;
cnt0--;
}
while (cnt1 > 0) {
arr[i++] = 1;
cnt1--;
}
while (cnt2 > 0) {
arr[i++] = 2;
cnt2--;
}
printArr(arr, n);
}
int main()
{
int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
int n = sizeof(arr) / sizeof(int);
sortArr(arr, n);
return 0;
}