Skip to content

Rework Bubble Sort C++ #251

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

Closed
wants to merge 3 commits into from
Closed

Rework Bubble Sort C++ #251

wants to merge 3 commits into from

Conversation

benchislett
Copy link
Contributor

Bring on the criticism!

@june128 june128 added the Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.) label Jul 16, 2018
#include <iostream>
#include <iterator>
#include <random>
#include <stdlib.h> // rand
Copy link
Contributor

Choose a reason for hiding this comment

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

do

#include <cstdlib>

}

for (;;) {
std::vector<int> bubble_sort(std::vector<int> unsorted_list) {
Copy link
Contributor

@mika314 mika314 Jul 16, 2018

Choose a reason for hiding this comment

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

I would do sort in place

void bubble_sort(std::vector<int>& array)

It will make code more performant (one array copy less) without any degradation of readability.

auto input = generate_input(10, rng);
int main()
{
std::vector<int> to_be_sorted = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

style minor: = {} just a noise, do:

std::vector<int> to_be_sorted;

{
std::vector<int> to_be_sorted = {};
// Initialize and print a vector with 50 random integers of domain [0,1000]
for (int i = 0; i < 50; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

std::vector<int> to_be_sorted = {};
// Initialize and print a vector with 50 random integers of domain [0,1000]
for (int i = 0; i < 50; i++) {
int value = rand() % 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

You may want to initialize random generator with

srand(time(nullptr));


bubble_sort(begin(input), end(input));
for (int i : sorted) { std::cout << i << " "; }
Copy link
Contributor

Choose a reason for hiding this comment

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

add

std::cout << std::endl; 

It is kind of annoying if terminal program does not do it.


std::cout << "\nafter sorting:\n";
print_range(std::cout, begin(input), end(input));
return 0;
Copy link
Contributor

@mika314 mika314 Jul 16, 2018

Choose a reason for hiding this comment

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

style minor: C++ does not require return 0

Really minor, I lot of people prefer return 0, for my taste it is just a noise.

using std::swap;
swap(*it, *(it + 1));
// Sweep through the array
for (unsigned int i = 0; i < unsorted_list.size() - 1; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

int main()
{
std::vector<int> to_be_sorted = {};
// Initialize and print a vector with 50 random integers of domain [0,1000]
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment is a lie, should be:

// Initialize and print a vector with 50 random integers of domain [0,999]

or

// Initialize and print a vector with 50 random integers of domain [0,1000)

First is preferable.

{
std::vector<int> to_be_sorted = {};
// Initialize and print a vector with 50 random integers of domain [0,1000]
for (int i = 0; i < 50; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would provide a hand written test sample. Random test set usually does not cover edge cases and gives false confidence about the code.

In this case it does not cover cases with empty array, already sorted array and array with reverse order for instance.

@mika314
Copy link
Contributor

mika314 commented Jul 16, 2018

@Butt4cak3
Copy link
Contributor

Butt4cak3 commented Jul 16, 2018

I don't really have any criticism for your code, but I can tell you that we already have an open pull request for Bubble Sort in C++, which is #244. We can't possible merge both of them because they will certainly cause merge conflicts.

Because we use a "first come - first serve" approach, I'm going to have to close this issue. I'm sorry for that, @benchislett.

We still appreciate your contribution, though! I hope that you will stay around and find something else you can do for the Algorithm Archive. Just next time, make sure that there's not already a PR that does the same thing. ;)

@Butt4cak3 Butt4cak3 closed this Jul 16, 2018
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.

4 participants