-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathDNF_sort.cpp
More file actions
42 lines (42 loc) · 792 Bytes
/
DNF_sort.cpp
File metadata and controls
42 lines (42 loc) · 792 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
#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 = 0;
int mid = 0;
int 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,mid,high);
high--;
}
}
}
int main(){
int n;
cout<<"Enter the size of array - ";
cin>>n;
int arr[n];
cout<<"Enter the elements of array - ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
dnfsort(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}