-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathDNF_Sort.cpp
More file actions
64 lines (41 loc) · 1.04 KB
/
Copy pathDNF_Sort.cpp
File metadata and controls
64 lines (41 loc) · 1.04 KB
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
61
62
63
64
#include<bits/stdc++.h>
#include<string.h>
#include<math.h>
using namespace std;
void DNF_SORT(int arr[], int arr_size)
{
int low = 0;
int high = arr_size - 1;
int mid = 0;
while (mid <= high)
{
switch (arr[mid])
{
case 0:
swap(arr[low++], arr[mid++]);
break;
case 1:
mid++;
break;
case 2:
swap(arr[mid], arr[high--]);
break;
}
}
}
void printArray(int arr[], int arr_size)
{
for (int i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = {0,0,1,2,0,1,2};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Array before running the algorithm: ";
printArray(arr, n);
DNF_SORT(arr, n);
cout << "\nArray after DNFS algorithm: ";
printArray(arr, n);
return 0;
}