-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdnfSort.cpp
More file actions
50 lines (41 loc) · 785 Bytes
/
dnfSort.cpp
File metadata and controls
50 lines (41 loc) · 785 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
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
// DNF SORT
// ------------------------------------------------------------------------
// Sorting in ascending order
void dnfsort(int a[], int n)
{
int low = 0;
int mid = 0;
int high = n - 1;
while (mid <= high)
{
if (a[mid] == 0)
{
swap(a[low], a[mid]);
low++;
mid++;
}
else if (a[mid] == 1)
{
mid++;
}
else
{
swap(a[mid], a[high]);
high--;
}
}
}
// Driver function
int main()
{
int a[] = {1, 0, 1, 2, 0, 1, 1};
int n = sizeof(a) / sizeof(int);
dnfsort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
return 0;
}