Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Dutch_National_Flag_(DNF)_Algo.md #1175

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

shimmer12
Copy link

No description provided.

@jxu
Copy link
Contributor

jxu commented Oct 20, 2023

Interesting algorithm. I've never heard of it. My intuitive solution would be to use a deque, which is pretty much what this algorithm does but in-place.

Copy link
Member

@jakobkogler jakobkogler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @shimmer12

Thanks for the PR. That article could be a nice addition to our site.

Before I accept it, please fix up a couple of things:

Rendering

Make it render nicely. You can copy-paste the article into https://cp-algorithms.com/preview.html to check how it looks. Currently it looks really bad. A couple of things:
- No header # Dutch National Flag algorithm is missing.
- Add a code block around the code ```cpp ... ``` (e.g. see the source code of the other articles).
- The enumeration doesn't work.
- Use Latex for the math bits, e.g. $O(N \log N)$ instead of just O(NlogN), the first one will render as $O(N \log N)$.
- Use some newlines to separate out thought to make it easier to read. Currently all the text is very stuck together.

Code

Remove reading/writing input/output. Makes the code shorter, and it doesn't add any new value to the article.

Explanation

I don't particularly like the explanation of the algorithm. You are just describing what the code does, but you don't explain why it does it. E.g.

  • you tell that the algorithm maintains 3 regions. But where are those regions? Are they all at the beginning? Are some at the beginning and some at the end?
  • You tell that you swap the element at the low pointer with the mid pointer, and increase both. But why does that help solve the problem?

I would find it better to describe exactly what the 3 pointers represent and how the array looks. And then explain the cases.
E.g. by telling you maintain 4 sections, first a section of just 0s, then a section of 1s, than the section of still unknown numbers and then the section of 2s at the end.
The low pointer points to the end of the 0s (i.e. to the start of the 1s section), the mid pointer points to the first unknown number, and high points to the last unknown number.

You can even add a graphic that shows this:

$$\begin{array}{ccccccccccc} 0 & 0 & 1 & 1 & 1 & ? & \dots & ? & 2 & 2 & 2 \\ & & \uparrow & & & \uparrow && \uparrow && \\ & & \text{low} & & & \text{mid} & & \text{high} & & & \end{array}$$

This was generated via Latex, and you can copy-paste it:

$$\begin{array}{ccccccccccc}
  0 & 0 & 1 & 1 & 1 & ? & \dots & ? & 2 & 2 & 2 \\
  & & \uparrow & & & \uparrow && \uparrow && \\
  & & \text{low} & & & \text{mid} & & \text{high} & & &
\end{array}$$

And then explain the cases. Something along the lines of:

We look at the first unknown number at position mid.

  • If it is a 0, we have to add it to the first block. We can extend the first block by swapping it with the number at position low which is a 1. So that also doesn't destroy the block of 1s, and we can adjust or two pointers.
  • If it's a 1, then it is already on the correct place and we can move the pointer forward.
  • ...

Other things

Since this is a new article, you should additionally add the following to the top of the page:

---
tags:
  - Original
---

and also add it to the navigation.md file and to the Readme.md file.

@jxu
Copy link
Contributor

jxu commented Oct 22, 2023

The article as written is way too verbose. If I understand correctly, the algorithm can be summarized as: iterate through elements, building two lists/stacks in-place on either end of the array by swapping of 0s at the left and 2s at the right. What's leftover in the middle is 1s.

I like the picture a lot but I'm not sure if any more formal proof adds any understanding. The only slightly confusing thing is that swapping a 2 for a 0 might require another swap, but it's easy to see the built-up 0 and 2 lists are correct.

@jxu
Copy link
Contributor

jxu commented Oct 22, 2023

Again if I understand correctly, the while loop can be turned into a simple for loop just by checking for 2s first? If there's only three possible values, then you could also just count how many of each element there are and make another pass to build the sorted array?

@jakobkogler
Copy link
Member

If there's only three possible values, then you could also just count how many of each element there are and make another pass to build the sorted array?

@jxu I've actually tried that yesterday, and wrote some small benchmark for it. But it turns out, that the Dutch algorithm is a lot faster (like 2x) then a counting sort. Was a bit unexpected for me, but I guess it has to do with the fact that it only needs one pass over the data, and therefore only needs to load the array once.

@jxu
Copy link
Contributor

jxu commented Oct 23, 2023

Did you benchmark it against the standard library's std::sort? Its time complexity is worse, but the implementation could be faster due to optimization for certain cases and cache and what not.

@jakobkogler
Copy link
Member

jakobkogler commented Oct 24, 2023

@jxu I did, it was something like this:

complexity

The dutch algorithm is very cache efficient.

@shimmer12
Copy link
Author

shimmer12 commented Oct 24, 2023

i have done changes accordingly, kindly take a look. if looks good please merge under hacktoberfest label @jakobkogler

@jakobkogler
Copy link
Member

jakobkogler commented Oct 26, 2023

@shimmer12 You should still explain a bit, where the pointers point to.
Just copy-pasting that formula doesn't explain what the pointers are actually doing.

Something like that:

I would find it better to describe exactly what the 3 pointers represent and how the array looks. And then explain the cases.
E.g. by telling you maintain 4 sections, first a section of just 0s, then a section of 1s, than the section of still unknown numbers and then the section of 2s at the end.
The low pointer points to the end of the 0s (i.e. to the start of the 1s section), the mid pointer points to the first unknown number, and high points to the last unknown number.

Also, what's up with the images?
Have you looked at the first image and understand what it represents? That image is from an university assignment where the task was to compare different approaches to solving the Dutch Flag problem. Your article however doesn't discuss different approaches, so the image doesn't fit. (Your solution is corresponds only one of the invariants, but you never mention that or even talk about invariants...).

The second picture is nice, however (and this also applies to the first one), we can't just reuse pictures from other people without asking. Everything has it's copyright, and presenting other peoples work under our website is not allowed. So we can't use any of those images.

Which is also fine, as we don't wanna 3 pictures of the same thing. I think, just using that math formula that I created is completely enough, if you describe it a bit...

Copy link

Visit the preview URL for this PR (for commit 9d95853):

https://cp-algorithms--preview-1175-q3gc7szw.web.app

(expires 2023-12-27T02:11:38.057946733Z)

@adamant-pwn
Copy link
Member

adamant-pwn commented Dec 20, 2023

@shimmer12 hi, are you still working on the article? Please also don't forget to add it into

P.S. Current preview link: https://cp-algorithms--preview-1175-q3gc7szw.web.app/others/Dutch_National_Flag_(DNF)_Algo.html

@shimmer12
Copy link
Author

@adamant-pwn Hii, I need to add some changes which I will do by this week, actually it was delayed due to my finals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants