/
dnf_sort.c
48 lines (45 loc) · 940 Bytes
/
dnf_sort.c
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
#include<stdio.h>
void dnfSort(int arr[],int n){
int t1,t2;
int s = 0;
int mid = 0;
int e = n-1;
while(mid<=e){
// shifting all the zeros to the front
if(arr[mid] == 0){
t1=arr[mid];
arr[mid]=arr[s];
arr[s]=t1;
s++;
mid++;
}
// when the array encounters 1, the position dosen't change
else if(arr[mid] == 1){
mid++;
}
// shifting all 2s to the end
else if(arr[mid] == 2){
t2=arr[mid];
arr[mid]=arr[e];
arr[e]=t2;
e--;
}
}
return;
}
int main(){
int n;
scanf("%d",&n);
int arr[1000];
for (int i = 0; i < n; ++i)
{
scanf("%d",&arr[i]);
}
dnfSort(arr, n);
for (int i = 0; i < n; ++i)
{
printf("%d",arr[i]);
printf(" ");
}
return 0;
}