Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

How to merge sort on a dual core ?

Thread view: 
John - 10 Mar 2008 15:57 GMT
Hi people !!

Do you know where i can find a merge sort source code designed for (at least 2) multi threaded environment ?
Roedy Green - 10 Mar 2008 23:07 GMT
>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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 10 Mar 2008 23:12 GMT
On Mon, 10 Mar 2008 22:07:06 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

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

you could use Sun's sort on both halves, or any  of the sorts with
source at http://mindprod.com/jgloss/sort.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 10 Mar 2008 23:14 GMT
On Mon, 10 Mar 2008 22:07:06 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

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

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
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
John - 11 Mar 2008 10:02 GMT
Roedy Green a écrit :
> On Mon, 10 Mar 2008 22:07:06 GMT, Roedy Green
> <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
> someone who said :
>
>> A simple one would just partition the data in half, sort each half on
>> a separate thread, then do a final merge pass.

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 ?
Roedy Green - 11 Mar 2008 11:11 GMT
>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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.