Skip to content

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

Merged
merged 2 commits into from
Jul 22, 2018

Conversation

mika314
Copy link
Contributor

@mika314 mika314 commented Jul 12, 2018

  • remove random generator function
  • use nested loop instead of is_sorted var

@Butt4cak3
Copy link
Contributor

Butt4cak3 commented Jul 12, 2018

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.

And that's it for the simplest bubble sort method. Now, as you might imagine, computer scientists have optimized this to the fiery lakes of Michigan and back, so we'll come back to this in the future and talk about how to optimize it. For now, it's fine to just bask in the simplicity that is bubble sort.

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 git mv to avoid that.

I think that's all I have to say.

@Gathros Gathros added the Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.) label Jul 12, 2018
@mika314
Copy link
Contributor Author

mika314 commented Jul 12, 2018

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.

@Butt4cak3
Copy link
Contributor

It's different, not closer. It doesn't have the is_sorted variable, but it has a third nested loop, which the Julia implementation also doesn't have. In addition to that, I'm pretty sure your implementation is even slower than the standard one without any optimization.

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.

@mika314
Copy link
Contributor Author

mika314 commented Jul 13, 2018

Third nested loop is just a bug. I pushed updated version.

  • fix the bug with netsted loop
  • renamed back bubblesort.cpp
  • match algorithm with Jilia

swap(*it, *(it + 1));
is_sorted = false;
if (*(it2 + 1) < *it2) {
std::swap(*it2, *(it2 + 1));
Copy link
Contributor

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);

Copy link
Contributor Author

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"

@mika314
Copy link
Contributor Author

mika314 commented Jul 15, 2018

BUMP

@leios
Copy link
Member

leios commented Jul 15, 2018

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?

@mika314 mika314 force-pushed the bubble_sort branch 2 times, most recently from 737a20f to 8da692c Compare July 16, 2018 04:57
@mika314 mika314 changed the title Cleanup bubble sort code for C and C++ Cleanup bubble sort code for C++ Jul 16, 2018
@mika314
Copy link
Contributor Author

mika314 commented Jul 16, 2018

This PR now is only C++.
C implemenation you can find here: #254

zsparal
zsparal previously approved these changes Jul 19, 2018
@mika314
Copy link
Contributor Author

mika314 commented Jul 20, 2018

@Gustorn can you please re-approve this PR, because I rebased it to fix automerge

zsparal
zsparal previously approved these changes Jul 20, 2018
@@ -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" %}
Copy link
Contributor

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.

Copy link
Contributor Author

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
@Butt4cak3
Copy link
Contributor

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 Butt4cak3 merged commit 3783a49 into algorithm-archivists:master Jul 22, 2018
@mika314
Copy link
Contributor Author

mika314 commented Jul 23, 2018

@Butt4cak3 Got it, from now I'll make commits without squish and rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants