How to merge sort on a dual core ?

J

John

Hi people !!

Do you know where i can find a merge sort source code designed for (at least 2) multi threaded environment ?
 
R

Roedy Green

Do you know where i can find a merge sort source code designed for (at least 2) multi threaded environment ?

A simple one would just partition the data in half, sort each half on
a separate thread, then do a final merge pass.
 
J

John

Roedy Green a écrit :
Yeah that's only idea i got.
A 2-way merge where you know in advance the sizes of the two inputs is
pretty simple, but if you need inspiration see
http://mindprod.com/products2.html#SORTED

There is no merge sort on your webpage.

By "2-way merge", do you mean iterating through the 2 partition at the same time, taking the smaller of each list ?
 
R

Roedy Green

There is no merge sort on your webpage.
There is now.
By "2-way merge", do you mean iterating through the 2 partition at the same time, taking the smaller of each list ?
yes, an sequential pass interleave, what a card collating machine used
to do in the olden days.

A mergesort sorts bundles by any of a number of means. Then it merges
the bundles, with very simple copy loop. The code in
http://mindprod.com/products2.html#SORTED is full of merge loops.

You don't use merge sorts for sorting a small number of items.
Normally the only time you use them is for external sorts, where you
sort ram-fulls of data, put them on disk, then read N bundles
simultaneously sequentially and output a combined bundle, gradually
reducing the number of bundles.

This takes me back to the 60s when I wrote a mag tape sort.
Back then much of your logic was about trying to get the final pass to
come out right to land on the desired tape. You used several reels for
scratch. It is much easier with disk, but for a fast sort, each
scratch should be on a different physical drive.

In your case, it is duck simple. Split your data logically in two,
(experiment to see if copying into two separate arrays helps or
hinders). Turn a Sun sort loose on both halves. Wait until both
threads are done. Merge the two outputs into one, and return that
array, or copy it back into the original with System.arraycopy.
If you write a sort for a List, e.g. ArrayList, with proper generics,
it will work on collections of any type that supports Comparable or
Comparator. To see how to pull it off, have a look at the source for
any of my sorts, or Sun’s sort.

However, because of Java’s lack of orthogonality, your List sort won’t
work for arrays of such Objects. You need to write very similar code
to do that. Even that array version won’t sort an array of primitives
such as long, int or byte. You have to write yet another slightly
different version of the sort to handle each type of primitive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top