Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions DNF sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<iostream>
using namespace std;
//divide array in 3 region 0,1 and 2
//check value of array if 0 then swap arr[low] and arr[mid], low++ and mid++
//if 1 mid++ //if 2 swap arr[mid] and arr[high],high--
//this sort is only for 0,1 and 2
void swap(int arr[],int i,int j){
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void dnf(int arr[],int n){
int l=0;
int m=0;
int h=n-1;
while(m<=h){
if(arr[m]==0){
swap(arr,l,m);
l++;
m++;
}
else if(arr[m]==2){
swap(arr,m,h);
h--;
}
else{
m++;
}
}
}
int main(){
int n ;
cout<<"Enter size of array ";
cin>>n;
int arr[n];
cout<<"Enter values of array ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
dnf(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
//time complexity of dnf sort is o(n)