C++ parallel merge sortParallel Merge SortMerge Sort in C++Merge Sort algorithmParallel Merge Sort in ScalaRun-aware merge sortSlow merge sortParallel merge sort in JavaMultithreaded bottom up merge sortC++ Merge Sort ImplementationC++: Merge Sort Implementation
OP Amp not amplifying audio signal
What's the meaning of "Sollensaussagen"?
Send out email when Apex Queueable fails and test it
Do creatures with a speed 0ft., fly 30ft. (hover) ever touch the ground?
Could the museum Saturn V's be refitted for one more flight?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
Pact of Blade Warlock with Dancing Blade
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
How exploitable/balanced is this homebrew spell: Spell Permanency?
Bullying boss launched a smear campaign and made me unemployable
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
How to show a landlord what we have in savings?
Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)
Knowledge-based authentication using Domain-driven Design in C#
Why are UK visa biometrics appointments suspended at USCIS Application Support Centers?
Processor speed limited at 0.4 Ghz
How do conventional missiles fly?
Mathematica command that allows it to read my intentions
What is the most common color to indicate the input-field is disabled?
Machine learning testing data
Getting extremely large arrows with tikzcd
What does the same-ish mean?
How to stretch the corners of this image so that it looks like a perfect rectangle?
C++ parallel merge sort
Parallel Merge SortMerge Sort in C++Merge Sort algorithmParallel Merge Sort in ScalaRun-aware merge sortSlow merge sortParallel merge sort in JavaMultithreaded bottom up merge sortC++ Merge Sort ImplementationC++: Merge Sort Implementation
$begingroup$
I'm a first year computer engineering student who's learning about algorithms and data structures. I've implemented a parallel merge sort algorithm in C++ and would like constructive criticism. This was done in Visual Studio on Windows.
Some thoughts:
- portability isn't a concern I have atm.
merge_sortisn't using tail recursion, which I would like it to do
function merge:
template<typename T>
void merge(T sequence[], int size)
T* sorted = new T[size];
int middle = size / 2;
int index_left = 0;
int index_right = middle;
int index_sequence = 0;
while (index_left < middle && index_right < size)
if (sequence[index_left] < sequence[index_right])
sorted[index_sequence++] = sequence[index_left++];
else
sorted[index_sequence++] = sequence[index_right++];
while (index_left < middle)
sorted[index_sequence++] = sequence[index_left++];
while (index_right < size)
sorted[index_sequence++] = sequence[index_right++];
for (int i = 0; i < size; i++)
sequence[i] = sorted[i];
delete[] sorted;
function merge_sort:
template<typename T, int min_size_to_thread = 10000>
void merge_sort(T sequence[], int size)
if (size > 1)
int middle = size / 2;
if (size > min_size_to_thread)
std::thread left(merge_sort<T, min_size_to_thread>, &sequence[0], middle);
std::thread right(merge_sort<T, min_size_to_thread>, &sequence[middle], size - middle);
left.join();
right.join();
else
merge_sort<T, min_size_to_thread>(&sequence[0], middle);
merge_sort<T, min_size_to_thread>(&sequence[middle], size - middle);
merge<T>(sequence, size);
For those who are interested: I Did some performance testing on a i5-2530M 2.50Ghz (2 cores).
The sequence to merge sort is int[100000]

c++ beginner multithreading sorting mergesort
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I'm a first year computer engineering student who's learning about algorithms and data structures. I've implemented a parallel merge sort algorithm in C++ and would like constructive criticism. This was done in Visual Studio on Windows.
Some thoughts:
- portability isn't a concern I have atm.
merge_sortisn't using tail recursion, which I would like it to do
function merge:
template<typename T>
void merge(T sequence[], int size)
T* sorted = new T[size];
int middle = size / 2;
int index_left = 0;
int index_right = middle;
int index_sequence = 0;
while (index_left < middle && index_right < size)
if (sequence[index_left] < sequence[index_right])
sorted[index_sequence++] = sequence[index_left++];
else
sorted[index_sequence++] = sequence[index_right++];
while (index_left < middle)
sorted[index_sequence++] = sequence[index_left++];
while (index_right < size)
sorted[index_sequence++] = sequence[index_right++];
for (int i = 0; i < size; i++)
sequence[i] = sorted[i];
delete[] sorted;
function merge_sort:
template<typename T, int min_size_to_thread = 10000>
void merge_sort(T sequence[], int size)
if (size > 1)
int middle = size / 2;
if (size > min_size_to_thread)
std::thread left(merge_sort<T, min_size_to_thread>, &sequence[0], middle);
std::thread right(merge_sort<T, min_size_to_thread>, &sequence[middle], size - middle);
left.join();
right.join();
else
merge_sort<T, min_size_to_thread>(&sequence[0], middle);
merge_sort<T, min_size_to_thread>(&sequence[middle], size - middle);
merge<T>(sequence, size);
For those who are interested: I Did some performance testing on a i5-2530M 2.50Ghz (2 cores).
The sequence to merge sort is int[100000]

c++ beginner multithreading sorting mergesort
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Isstd::thread rightredundant?
$endgroup$
– user644361
1 hour ago
$begingroup$
I've tested:merge_sortruns faster without threadingright, so it's better to only threadleftand let right run in the main thread.
$endgroup$
– user644361
1 hour ago
add a comment |
$begingroup$
I'm a first year computer engineering student who's learning about algorithms and data structures. I've implemented a parallel merge sort algorithm in C++ and would like constructive criticism. This was done in Visual Studio on Windows.
Some thoughts:
- portability isn't a concern I have atm.
merge_sortisn't using tail recursion, which I would like it to do
function merge:
template<typename T>
void merge(T sequence[], int size)
T* sorted = new T[size];
int middle = size / 2;
int index_left = 0;
int index_right = middle;
int index_sequence = 0;
while (index_left < middle && index_right < size)
if (sequence[index_left] < sequence[index_right])
sorted[index_sequence++] = sequence[index_left++];
else
sorted[index_sequence++] = sequence[index_right++];
while (index_left < middle)
sorted[index_sequence++] = sequence[index_left++];
while (index_right < size)
sorted[index_sequence++] = sequence[index_right++];
for (int i = 0; i < size; i++)
sequence[i] = sorted[i];
delete[] sorted;
function merge_sort:
template<typename T, int min_size_to_thread = 10000>
void merge_sort(T sequence[], int size)
if (size > 1)
int middle = size / 2;
if (size > min_size_to_thread)
std::thread left(merge_sort<T, min_size_to_thread>, &sequence[0], middle);
std::thread right(merge_sort<T, min_size_to_thread>, &sequence[middle], size - middle);
left.join();
right.join();
else
merge_sort<T, min_size_to_thread>(&sequence[0], middle);
merge_sort<T, min_size_to_thread>(&sequence[middle], size - middle);
merge<T>(sequence, size);
For those who are interested: I Did some performance testing on a i5-2530M 2.50Ghz (2 cores).
The sequence to merge sort is int[100000]

c++ beginner multithreading sorting mergesort
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I'm a first year computer engineering student who's learning about algorithms and data structures. I've implemented a parallel merge sort algorithm in C++ and would like constructive criticism. This was done in Visual Studio on Windows.
Some thoughts:
- portability isn't a concern I have atm.
merge_sortisn't using tail recursion, which I would like it to do
function merge:
template<typename T>
void merge(T sequence[], int size)
T* sorted = new T[size];
int middle = size / 2;
int index_left = 0;
int index_right = middle;
int index_sequence = 0;
while (index_left < middle && index_right < size)
if (sequence[index_left] < sequence[index_right])
sorted[index_sequence++] = sequence[index_left++];
else
sorted[index_sequence++] = sequence[index_right++];
while (index_left < middle)
sorted[index_sequence++] = sequence[index_left++];
while (index_right < size)
sorted[index_sequence++] = sequence[index_right++];
for (int i = 0; i < size; i++)
sequence[i] = sorted[i];
delete[] sorted;
function merge_sort:
template<typename T, int min_size_to_thread = 10000>
void merge_sort(T sequence[], int size)
if (size > 1)
int middle = size / 2;
if (size > min_size_to_thread)
std::thread left(merge_sort<T, min_size_to_thread>, &sequence[0], middle);
std::thread right(merge_sort<T, min_size_to_thread>, &sequence[middle], size - middle);
left.join();
right.join();
else
merge_sort<T, min_size_to_thread>(&sequence[0], middle);
merge_sort<T, min_size_to_thread>(&sequence[middle], size - middle);
merge<T>(sequence, size);
For those who are interested: I Did some performance testing on a i5-2530M 2.50Ghz (2 cores).
The sequence to merge sort is int[100000]

c++ beginner multithreading sorting mergesort
c++ beginner multithreading sorting mergesort
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 mins ago
200_success
131k17156422
131k17156422
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 hours ago
user644361user644361
604
604
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user644361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
Isstd::thread rightredundant?
$endgroup$
– user644361
1 hour ago
$begingroup$
I've tested:merge_sortruns faster without threadingright, so it's better to only threadleftand let right run in the main thread.
$endgroup$
– user644361
1 hour ago
add a comment |
$begingroup$
Isstd::thread rightredundant?
$endgroup$
– user644361
1 hour ago
$begingroup$
I've tested:merge_sortruns faster without threadingright, so it's better to only threadleftand let right run in the main thread.
$endgroup$
– user644361
1 hour ago
$begingroup$
Is
std::thread right redundant?$endgroup$
– user644361
1 hour ago
$begingroup$
Is
std::thread right redundant?$endgroup$
– user644361
1 hour ago
$begingroup$
I've tested:
merge_sort runs faster without threading right, so it's better to only thread left and let right run in the main thread.$endgroup$
– user644361
1 hour ago
$begingroup$
I've tested:
merge_sort runs faster without threading right, so it's better to only thread left and let right run in the main thread.$endgroup$
– user644361
1 hour ago
add a comment |
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
);
);
user644361 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2f216746%2fc-parallel-merge-sort%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
user644361 is a new contributor. Be nice, and check out our Code of Conduct.
user644361 is a new contributor. Be nice, and check out our Code of Conduct.
user644361 is a new contributor. Be nice, and check out our Code of Conduct.
user644361 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2f216746%2fc-parallel-merge-sort%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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
$begingroup$
Is
std::thread rightredundant?$endgroup$
– user644361
1 hour ago
$begingroup$
I've tested:
merge_sortruns faster without threadingright, so it's better to only threadleftand let right run in the main thread.$endgroup$
– user644361
1 hour ago