Bubble sort in parallel
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
1
Have you tried to test it? Just usestd::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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
c++ multithreading
asked Jun 5 at 18:14
PeMaCN
1404
1404
1
Have you tried to test it? Just usestd::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
add a comment |
1
Have you tried to test it? Just usestd::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
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
How much energy can I measure for a 500-dimensional array?
New contributor
add a comment |
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?
New contributor
add a comment |
up vote
0
down vote
How much energy can I measure for a 500-dimensional array?
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
How much energy can I measure for a 500-dimensional array?
New contributor
How much energy can I measure for a 500-dimensional array?
New contributor
New contributor
answered 4 mins ago
onur sezer
1
1
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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