From 61250e74e454fc0884b61fe3acbfb73ba04f3ded Mon Sep 17 00:00:00 2001 From: Srishti Soni <92056170+shimmer12@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:31:03 +0530 Subject: [PATCH 1/4] Create Dutch_National_Flag_(DNF)_Algo.md --- src/others/Dutch_National_Flag_(DNF)_Algo.md | 64 ++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/others/Dutch_National_Flag_(DNF)_Algo.md diff --git a/src/others/Dutch_National_Flag_(DNF)_Algo.md b/src/others/Dutch_National_Flag_(DNF)_Algo.md new file mode 100644 index 000000000..1ac2356ee --- /dev/null +++ b/src/others/Dutch_National_Flag_(DNF)_Algo.md @@ -0,0 +1,64 @@ +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 and without using additional space. Memory. This algorithm is often used in situations where it is necessary to sort an array of integers 0, 1, and 2. The DNF algorithm is especially useful when you want to sort data with three distinct values ​​and need to minimize complexity and time. Use of space. . +## Dutch flag problem +The Dutch flag algorithm takes its name from a classic problem. Imagine you have a Dutch national flag made up of three horizontal stripes, one white, one red and one blue, running 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 can be stated as follows: +You are given an array that contains only the integers 0, 1, and 2. Your task is to sort this array in a way that all the 0s come first, followed by all the 1s, and then all the 2s. The challenge is to do this in an efficient and in-place manner without using additional memory space or a sorting function. +## Brute Force and Inefficient Solutions +Before diving 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 an interview 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 will help you process 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. +#include +#include + +void dutchFlagSort(std::vector& 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--; + } + } +} + +int main() { + std::vector nums = {2, 0, 1, 2, 0, 1, 2}; + dutchFlagSort(nums); + + std::cout << "Sorted array: "; + for (int num : nums) { + std::cout << num << " "; + } + std::cout << std::endl; + + return 0; +} + + +This code shows the algorithm in action, sorting an array containing 0, 1, and 2 according to the rules. +## 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 is because it processes each element one at a time and performs one pass through the array. The algorithm is designed to minimize unnecessary operations and transactions. +The space complexity of the Dutch national flags algorithm is O(1) because it uses only +. In summary, the Dutch national flags algorithm is a valuable tool for efficient sorting an array has 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 quest. constant amount of additional space to store three pointers (low, medium, high). It does not require any additional data structures or memory allocations, making it an in-place sorting algorithm. + + + From 29a4a0a37899cc05898389c363473e4e6a004c89 Mon Sep 17 00:00:00 2001 From: Srishti Soni <92056170+shimmer12@users.noreply.github.com> Date: Tue, 24 Oct 2023 22:47:18 +0530 Subject: [PATCH 2/4] Update Dutch_National_Flag_(DNF)_Algo.md added required changes --- src/others/Dutch_National_Flag_(DNF)_Algo.md | 79 ++++++++++---------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/others/Dutch_National_Flag_(DNF)_Algo.md b/src/others/Dutch_National_Flag_(DNF)_Algo.md index 1ac2356ee..319edf5ac 100644 --- a/src/others/Dutch_National_Flag_(DNF)_Algo.md +++ b/src/others/Dutch_National_Flag_(DNF)_Algo.md @@ -1,24 +1,37 @@ -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 and without using additional space. Memory. This algorithm is often used in situations where it is necessary to sort an array of integers 0, 1, and 2. The DNF algorithm is especially useful when you want to sort data with three distinct values ​​and need to minimize complexity and time. Use of space. . -## Dutch flag problem -The Dutch flag algorithm takes its name from a classic problem. Imagine you have a Dutch national flag made up of three horizontal stripes, one white, one red and one blue, running 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 can be stated as follows: -You are given an array that contains only the integers 0, 1, and 2. Your task is to sort this array in a way that all the 0s come first, followed by all the 1s, and then all the 2s. The challenge is to do this in an efficient and in-place manner without using additional memory space or a sorting function. -## Brute Force and Inefficient Solutions -Before diving 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 an interview 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 will help you process 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. -#include + +# 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. +![image](https://github.com/shimmer12/cp-algorithms/assets/92056170/32906482-4f62-4c51-8d8d-fe6a0e279814) + +![image](https://github.com/shimmer12/cp-algorithms/assets/92056170/fc9dc112-5d36-42f2-bb06-4347a371e00c) + + +```cpp #include void dutchFlagSort(std::vector& nums) { @@ -39,26 +52,12 @@ void dutchFlagSort(std::vector& nums) { } } } +``` -int main() { - std::vector nums = {2, 0, 1, 2, 0, 1, 2}; - dutchFlagSort(nums); - - std::cout << "Sorted array: "; - for (int num : nums) { - std::cout << num << " "; - } - std::cout << std::endl; - - return 0; -} - - -This code shows the algorithm in action, sorting an array containing 0, 1, and 2 according to the rules. -## 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 is because it processes each element one at a time and performs one pass through the array. The algorithm is designed to minimize unnecessary operations and transactions. -The space complexity of the Dutch national flags algorithm is O(1) because it uses only -. In summary, the Dutch national flags algorithm is a valuable tool for efficient sorting an array has 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 quest. constant amount of additional space to store three pointers (low, medium, high). It does not require any additional data structures or memory allocations, making it an in-place sorting algorithm. +## 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. +--- From ea2813876bbda279c3e870e753780333cca8649a Mon Sep 17 00:00:00 2001 From: Srishti Soni <92056170+shimmer12@users.noreply.github.com> Date: Tue, 24 Oct 2023 22:53:25 +0530 Subject: [PATCH 3/4] Update Dutch_National_Flag_(DNF)_Algo.md --- src/others/Dutch_National_Flag_(DNF)_Algo.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/others/Dutch_National_Flag_(DNF)_Algo.md b/src/others/Dutch_National_Flag_(DNF)_Algo.md index 319edf5ac..030ec7279 100644 --- a/src/others/Dutch_National_Flag_(DNF)_Algo.md +++ b/src/others/Dutch_National_Flag_(DNF)_Algo.md @@ -30,6 +30,12 @@ The Dutch National Flag algorithm provides an efficient and elegant solution to ![image](https://github.com/shimmer12/cp-algorithms/assets/92056170/fc9dc112-5d36-42f2-bb06-4347a371e00c) +$$\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 @@ -61,3 +67,7 @@ The Dutch National Flag algorithm operates with O(N) time complexity, where N is 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. --- +--- +tags: + - Original +--- From 9d9585346aac6d23c48c661d5bd89abe698594e9 Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov Date: Wed, 20 Dec 2023 03:09:09 +0100 Subject: [PATCH 4/4] change to deploy preview --- src/others/Dutch_National_Flag_(DNF)_Algo.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/others/Dutch_National_Flag_(DNF)_Algo.md b/src/others/Dutch_National_Flag_(DNF)_Algo.md index 030ec7279..191eb24e9 100644 --- a/src/others/Dutch_National_Flag_(DNF)_Algo.md +++ b/src/others/Dutch_National_Flag_(DNF)_Algo.md @@ -1,3 +1,7 @@ +--- +tags: + - Original +--- # Dutch National Flag Algorithm @@ -66,8 +70,3 @@ The Dutch National Flag algorithm operates with O(N) time complexity, where N is 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. ---- ---- -tags: - - Original ----