-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Description
Detailed description
DNF Sort
The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange white, red, and blue balls such that balls of the same color are placed together. Similarly, we design a problem statement consisting of an array of 0s,1s, and 2s. The aim is to sort the arrays in ascending/descending order.
Problem Statement:
Given an array A[] consisting of 0s, 1s, and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.
Complexity Analysis:
- Time Complexity: O(n).
Only one traversal of the array is needed.
- Space Complexity: O(1).
No extra space is required.
Context
Approach
The main context of this problem is to use the three-pointer method, (without storing the count of 0s,1s, and 2s in any data structure). In the three-pointer method, we track of following pointers:
- low - tracks the position of 0s in the array.
- mid - tracks all the elements in the array.
- high - tracks the position of 2s in the array.
This approach although not widely used, but still, take a part in Sorting Algortihms because this algorithm uses O (n) time complexity and O(1) Space Complexity, which is much better than other sorting Algos.
Possible implementation
No response
Additional information
No response