Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.8k
Create Dutch_National_Flag_(DNF)_Algo.md#1175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
shimmer12 wants to merge5 commits intocp-algorithms:mainChoose a base branch fromshimmer12:patch-1
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
61250e7
Create Dutch_National_Flag_(DNF)_Algo.md
shimmer1229a4a0a
Update Dutch_National_Flag_(DNF)_Algo.md
shimmer12ea28138
Update Dutch_National_Flag_(DNF)_Algo.md
shimmer129d95853
change to deploy preview
adamant-pwn0d1d942
Merge branch 'main' into patch-1
adamant-pwnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletionssrc/others/Dutch_National_Flag_(DNF)_Algo.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
tags: | ||
- Original | ||
--- | ||
# Dutch National Flag Algorithm | ||
The Dutch National Flag (DNF) algorithm, also known as the 3-way partition algorithm, is a powerful technique for efficiently sorting an array containing only three distinct elements without using additional memory space. This algorithm is often used in situations where you need to sort an array of integers with values 0, 1, and 2. The DNF algorithm is particularly valuable when you want to sort data with three distinct values while minimizing time and space complexity. | ||
## The Dutch Flag Problem | ||
The Dutch flag algorithm is named after a classic problem. Imagine you have a Dutch national flag consisting of three horizontal stripes: white, red, and blue, from top to bottom. The task is to arrange these colors in the correct order, such that all the white stripes are at the top, followed by the red stripes, and then the blue stripes at the bottom. This problem can be translated into sorting an array of 0s, 1s, and 2s, with 0 representing white, 1 representing red, and 2 representing blue. | ||
## The Problem Statement | ||
In the context of the Dutch National Flag algorithm, the problem is stated as follows: You are given an array that contains only the integers 0, 1, and 2. Your task is to sort this array so that all the 0s come first, followed by all the 1s, and then all the 2s. The challenge is to do this efficiently and in-place without using additional memory space or a sorting function. | ||
## Brute Force and Inefficient Solutions | ||
Before delving into the Dutch National Flag algorithm, it's essential to understand that the problem can be solved using traditional sorting algorithms like Merge Sort or Quick Sort. However, these algorithms have time complexities of O(NlogN) and often require extra memory for temporary storage. In interviews or competitive programming, these solutions would not be considered optimal. | ||
## The Dutch National Flag Algorithm | ||
The Dutch National Flag algorithm provides an efficient and elegant solution to this problem. It uses three pointers: 'low', 'mid', and 'high' to maintain the three regions in the array as required: 0s on the left, 1s in the middle, and 2s on the right. The algorithm iterates through the array, moving these pointers to reorganize the elements accordingly. Here are the main steps: | ||
1. Initialize the three pointers: 'low', 'mid', and 'high'. | ||
2. Use a while loop that continues as long as 'mid' is less than or equal to 'high'. This loop processes the elements within the array. | ||
3. Inside the loop: | ||
- If the element at the 'mid' pointer is 0, swap it with the element at the 'low' pointer and increment both 'low' and 'mid'. | ||
- If the element at the 'mid' pointer is 1, leave it in place (no swap) and increment 'mid'. | ||
- If the element at the 'mid' pointer is 2, swap it with the element at the 'high' pointer and decrement 'high'. | ||
4. Continue this process until 'mid' exceeds 'high'. At this point, the entire array is sorted with all the 0s on the left, followed by the 1s, and then the 2s on the right. | ||
 | ||
 | ||
$$\begin{array}{ccccccccccc} | ||
0 & 0 & 1 & 1 & 1 & ? & \dots & ? & 2 & 2 & 2 \\ | ||
& & \uparrow & & & \uparrow && \uparrow && \\ | ||
& & \text{low} & & & \text{mid} & & \text{high} & & & | ||
\end{array}$$ | ||
```cpp | ||
#include <vector> | ||
void dutchFlagSort(std::vector<int>& nums) { | ||
int low = 0; | ||
int mid = 0; | ||
int high = nums.size() - 1; | ||
while (mid <= high) { | ||
if (nums[mid] == 0) { | ||
std::swap(nums[low], nums[mid]); | ||
low++; | ||
mid++; | ||
} else if (nums[mid] == 1) { | ||
mid++; | ||
} else { | ||
std::swap(nums[mid], nums[high]); | ||
high--; | ||
} | ||
} | ||
} | ||
``` | ||
## Time and Space Complexity | ||
The Dutch National Flag algorithm operates with O(N) time complexity, where N is the length of the input array. This efficiency results from processing each element one at a time in a single pass through the array. The algorithm minimizes unnecessary operations and transactions. The space complexity is O(1) as it uses only a constant amount of additional space to store the three pointers (low, mid, high), without requiring additional data structures or memory allocations. | ||
In summary, the Dutch National Flag algorithm is a valuable tool for efficiently sorting an array with three distinct elements while minimizing time and space complexity. Understanding the thought process and intuition behind this algorithm can be a great advantage in interviews, competitive programming, and real-world applications where sorting three distinct values is a common requirement. | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.