-
-
Notifications
You must be signed in to change notification settings - Fork 360
Cleanup bubble sort code for C++ #244
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
Conversation
mika314
commented
Jul 12, 2018
- remove random generator function
- use nested loop instead of is_sorted var
I don't have much to do with the C and C++ code in this project, but I know that most other language implementations (C#, Java, JavaScript, Julia, Matlab, Python, Racket?, Rust, Swift) have no optimization whatsoever. The chapter also talks about the pure, unoptimized version of it, so I don't know why it's there in the first place.
I think all implementations should follow the same procedure, so I would actually vote to remove the optimization from all implementations to make it match both the chapter text and the Julia version, which sort of serves as the reference implementation for all chapters. Other than that, there was no reason to remove the random generator function. Random inputs to test the programs are fine. I also don't understand why you felt the need to rename the .cpp file. That seems to me like a purely preferential thing. We don't have a convention for how to name source files and I don't think we need one. In addition to that, you renamed it so that Git wasn't even able to track it. For future reference, you should try to always move files with I think that's all I have to say. |
My PR actually closer to Julia implementation, Julia implementation does not have is_sorted var. I removed random generation because implementation is confusing for inexperienced programmers and does not provide to much value. I renamed the file to make the name consistent with c implementation. |
It's different, not closer. It doesn't have the The implementation of the random generator is not subject of this chapter. It doesn't really matter as long as it works. Most of my other points still stand, but I'll let someone who actually knows C and C++ go ahead and properly review this. |
Third nested loop is just a bug. I pushed updated version.
|
swap(*it, *(it + 1)); | ||
is_sorted = false; | ||
if (*(it2 + 1) < *it2) { | ||
std::swap(*it2, *(it2 + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use std::iter_swap(it2, it2 + 1);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here commit "Use iter_swap instead of swap 6e44fdf"
BUMP |
Hey @AntonTe, sorry for taking so long to get to this PR. For the record BUMPs are not really necessary and lead to spam for everyone following the algorithm archive. Please avoid using them in the future, if you can. We have a lot of PR's and are getting to them when we have time. As far as this PR is concerned, it is difficult to review because it is basically an update to your personal preference here, which means we need to discuss style. It is hard to do so for both C and C++ at the same time. In this case, the C++ code is notably cleaner; however, I am not sure about the C code. Would it be possible to split the PR into 2? |
737a20f
to
8da692c
Compare
This PR now is only C++. |
@Gustorn can you please re-approve this PR, because I rebased it to fix automerge |
contents/bubble_sort/bubble_sort.md
Outdated
@@ -14,6 +14,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with | |||
[import:9-27, lang:"csharp"](code/csharp/BubbleSort.cs) | |||
{% sample lang="c" %} | |||
[import:11-21, lang:"c_cpp"](code/c/bubble_sort.c) | |||
{% sample lang="cpp" %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a C++ code import further down. No need to create another one. In fact, this causes the page to render incorrectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be good now.
- remove random generator function - use nested loop instead of is_sorted var - match the bubble sort C++ algorithm with Julia
The import propery works now. For the future, please don't amend your commits and force push after you made requested changes. It's helpful for us as reviewers to see only what you changed after the review, or else we have to look through the entire code again. Since @Gustorn already approved the code itself, I will go ahead and merge this one. |
@Butt4cak3 Got it, from now I'll make commits without squish and rebase. |