-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathDNF_sort.cpp
More file actions
50 lines (44 loc) · 798 Bytes
/
DNF_sort.cpp
File metadata and controls
50 lines (44 loc) · 798 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<iostream>
using namespace std;
void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void DNFsort(int arr[], int n)
{
int low,mid,high;
low = 0;
mid = 0;
high = n-1;
while(mid <= high)
{
if(arr[mid] == 0)
{
swap(arr,low,mid);
low++;
mid++;
}
else if(arr[mid] == 1)
{
mid++;
}
else
{
swap(arr,high,mid);
high--;
}
}
}
int main()
{
int arr[] = {1,0,2,1,0,1,2,1,2};
int s = sizeof(arr)/sizeof(int);
DNFsort(arr,s);
for (int i = 0; i < s; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}