Bubble sort in parallel











up vote
2
down vote

favorite
1












I have done bubble sort algorithm on a vector that is filled with randomly generated values. Bubble sort is actually done with odd-even transition method:



#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <thread>
#include <mutex>

void fill_with_random(std::vector<int> &vec)
{
constexpr int lower_bound = 1;
constexpr int upper_bound = 100;

std::random_device rnd_device;
std::mt19937 mersenne_engine(rnd_device());

std::uniform_int_distribution<int> distribution(lower_bound, upper_bound);

auto generator = std::bind(distribution, mersenne_engine);
std::generate(vec.begin(), vec.end(), generator);
}

bool is_odd(int number)
{
return number % 2 != 0;
}

int main(int argc, char *argv)
{
constexpr size_t vector_size = 10;

std::mutex mutex;

std::vector<int> vec(vector_size);
fill_with_random(vec);

std::cout << "Normal vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

for (size_t i = 0; i < vector_size; i++)
{
std::vector<std::thread> threads;
if (is_odd(i))
{
for (size_t j = 1; j < vector_size / 2 + vector_size % 2; j++)
{
size_t second = 2 * j;
size_t first = second - 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}
else
{
for (size_t j = 0; j < vector_size / 2; j++)
{
size_t first = 2 * j;
size_t second = first + 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}

for (auto& thread : threads)
{
thread.join();
}
}

std::cout << "Sorted vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

return 0;
}


Apart from style review, please check functionality of the algorithm itself.
Thanks in advance.










share|improve this question


















  • 1




    Have you tried to test it? Just use std::is_sorted() on your vector. You could write a loop that would check vectors up to certain size.
    – Incomputable
    Jun 5 at 18:47










  • I haven't made a unit test yet but I will.
    – PeMaCN
    Jun 6 at 6:59















up vote
2
down vote

favorite
1












I have done bubble sort algorithm on a vector that is filled with randomly generated values. Bubble sort is actually done with odd-even transition method:



#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <thread>
#include <mutex>

void fill_with_random(std::vector<int> &vec)
{
constexpr int lower_bound = 1;
constexpr int upper_bound = 100;

std::random_device rnd_device;
std::mt19937 mersenne_engine(rnd_device());

std::uniform_int_distribution<int> distribution(lower_bound, upper_bound);

auto generator = std::bind(distribution, mersenne_engine);
std::generate(vec.begin(), vec.end(), generator);
}

bool is_odd(int number)
{
return number % 2 != 0;
}

int main(int argc, char *argv)
{
constexpr size_t vector_size = 10;

std::mutex mutex;

std::vector<int> vec(vector_size);
fill_with_random(vec);

std::cout << "Normal vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

for (size_t i = 0; i < vector_size; i++)
{
std::vector<std::thread> threads;
if (is_odd(i))
{
for (size_t j = 1; j < vector_size / 2 + vector_size % 2; j++)
{
size_t second = 2 * j;
size_t first = second - 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}
else
{
for (size_t j = 0; j < vector_size / 2; j++)
{
size_t first = 2 * j;
size_t second = first + 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}

for (auto& thread : threads)
{
thread.join();
}
}

std::cout << "Sorted vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

return 0;
}


Apart from style review, please check functionality of the algorithm itself.
Thanks in advance.










share|improve this question


















  • 1




    Have you tried to test it? Just use std::is_sorted() on your vector. You could write a loop that would check vectors up to certain size.
    – Incomputable
    Jun 5 at 18:47










  • I haven't made a unit test yet but I will.
    – PeMaCN
    Jun 6 at 6:59













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have done bubble sort algorithm on a vector that is filled with randomly generated values. Bubble sort is actually done with odd-even transition method:



#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <thread>
#include <mutex>

void fill_with_random(std::vector<int> &vec)
{
constexpr int lower_bound = 1;
constexpr int upper_bound = 100;

std::random_device rnd_device;
std::mt19937 mersenne_engine(rnd_device());

std::uniform_int_distribution<int> distribution(lower_bound, upper_bound);

auto generator = std::bind(distribution, mersenne_engine);
std::generate(vec.begin(), vec.end(), generator);
}

bool is_odd(int number)
{
return number % 2 != 0;
}

int main(int argc, char *argv)
{
constexpr size_t vector_size = 10;

std::mutex mutex;

std::vector<int> vec(vector_size);
fill_with_random(vec);

std::cout << "Normal vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

for (size_t i = 0; i < vector_size; i++)
{
std::vector<std::thread> threads;
if (is_odd(i))
{
for (size_t j = 1; j < vector_size / 2 + vector_size % 2; j++)
{
size_t second = 2 * j;
size_t first = second - 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}
else
{
for (size_t j = 0; j < vector_size / 2; j++)
{
size_t first = 2 * j;
size_t second = first + 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}

for (auto& thread : threads)
{
thread.join();
}
}

std::cout << "Sorted vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

return 0;
}


Apart from style review, please check functionality of the algorithm itself.
Thanks in advance.










share|improve this question













I have done bubble sort algorithm on a vector that is filled with randomly generated values. Bubble sort is actually done with odd-even transition method:



#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <thread>
#include <mutex>

void fill_with_random(std::vector<int> &vec)
{
constexpr int lower_bound = 1;
constexpr int upper_bound = 100;

std::random_device rnd_device;
std::mt19937 mersenne_engine(rnd_device());

std::uniform_int_distribution<int> distribution(lower_bound, upper_bound);

auto generator = std::bind(distribution, mersenne_engine);
std::generate(vec.begin(), vec.end(), generator);
}

bool is_odd(int number)
{
return number % 2 != 0;
}

int main(int argc, char *argv)
{
constexpr size_t vector_size = 10;

std::mutex mutex;

std::vector<int> vec(vector_size);
fill_with_random(vec);

std::cout << "Normal vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

for (size_t i = 0; i < vector_size; i++)
{
std::vector<std::thread> threads;
if (is_odd(i))
{
for (size_t j = 1; j < vector_size / 2 + vector_size % 2; j++)
{
size_t second = 2 * j;
size_t first = second - 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}
else
{
for (size_t j = 0; j < vector_size / 2; j++)
{
size_t first = 2 * j;
size_t second = first + 1;

threads.emplace_back(
[&vec, first, second, &mutex]()
{
if (vec[first] > vec[second])
{
std::lock_guard<std::mutex> lock(mutex);
std::iter_swap(vec.begin() + first, vec.begin() + second);
}
}
);
}
}

for (auto& thread : threads)
{
thread.join();
}
}

std::cout << "Sorted vector: ";
for (auto item : vec)
{
std::cout << item << " ";
}
std::cout << std::endl;

return 0;
}


Apart from style review, please check functionality of the algorithm itself.
Thanks in advance.







c++ multithreading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 5 at 18:14









PeMaCN

1404




1404








  • 1




    Have you tried to test it? Just use std::is_sorted() on your vector. You could write a loop that would check vectors up to certain size.
    – Incomputable
    Jun 5 at 18:47










  • I haven't made a unit test yet but I will.
    – PeMaCN
    Jun 6 at 6:59














  • 1




    Have you tried to test it? Just use std::is_sorted() on your vector. You could write a loop that would check vectors up to certain size.
    – Incomputable
    Jun 5 at 18:47










  • I haven't made a unit test yet but I will.
    – PeMaCN
    Jun 6 at 6:59








1




1




Have you tried to test it? Just use std::is_sorted() on your vector. You could write a loop that would check vectors up to certain size.
– Incomputable
Jun 5 at 18:47




Have you tried to test it? Just use std::is_sorted() on your vector. You could write a loop that would check vectors up to certain size.
– Incomputable
Jun 5 at 18:47












I haven't made a unit test yet but I will.
– PeMaCN
Jun 6 at 6:59




I haven't made a unit test yet but I will.
– PeMaCN
Jun 6 at 6:59










1 Answer
1






active

oldest

votes

















up vote
0
down vote













How much energy can I measure for a 500-dimensional array?





share








New contributor




onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195904%2fbubble-sort-in-parallel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    How much energy can I measure for a 500-dimensional array?





    share








    New contributor




    onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      How much energy can I measure for a 500-dimensional array?





      share








      New contributor




      onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        How much energy can I measure for a 500-dimensional array?





        share








        New contributor




        onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        How much energy can I measure for a 500-dimensional array?






        share








        New contributor




        onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share


        share






        New contributor




        onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 4 mins ago









        onur sezer

        1




        1




        New contributor




        onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        onur sezer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195904%2fbubble-sort-in-parallel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'