![]() |
[Java + C] Merge Sort
Why is it that when I looked at the merge sort source code provided by several sorting alogrithms websites that contains a "Thread.sleep(something)" which whenever I use it, the merge sorter actually runs longer than insertion, selection or even bubble!
So, when I removed the Thread.sleep() part, the merge sort alogrithms just zip by as if it was never there while the thread.sleep is still there, it runs like its a friggin turttle... I know its because of the Thread.sleep() but comon, why is it even there in the first place?? |
I have no idea...if you're trying to make a fast sort the last thing you want to do is take it off the CPU completely. The only thing I can think of is that the sort could take a long time...and that could be bad on an OS with cooperative multitasking?!?! Does Java even run on any of those?
|
Java runs on pretty much anything it wants to...
I went back to the mergeSort alogrithms which is from the Microsystems and I found that mergeSort implemented a superclass that contained a method called "pause", and when I traced the pause back to the superclass and found that there was a Thread.sleep(20); line within the pause() method. I know that merge sort is one of the fastest sorter when it comes to large list but when I tested it on my machine, it just lagged like shit. So, I removed the thread and now..it runs like a beauty... Just strange to see that Microsystem is even using Thread.sleep() line within a mergeSort... While we're on the top of sorting alogrithms, I was messing around with Shell Sort and I noticed that as you increase the increments, the sorter is faster and so I just had the increment to match with the length of the array/vector and it runs at the fastest. So..just a tip for those of you who use a shell sort. Have the increment match the length of array or vector. |
Thread.sleep() definitely pauses the algorithm for that specified number of milliseconds, we've used it in class for some of the graphical things so they don't zoom right by.. Funny they put that into a sort- especially Microsystems, I hope somebody comes up with an answer to why this is so!
|
Merge sort can be parallelized with multiple threads. I haven't looked at the code, so I don't know if they are even doing that. It could be if they did set it up for multithreading and put the sleep in there so they didn't have to use synchronization? A really bad way of doing it that shouldn't work even.
|
Is it just as accurate without the sleeps? Also, how big a space are you sorting? And what kind of computer is this?
|
There is no benefit in parallelizing merge sort on a single processor...
Parallelizing merge sort can't be done properly with threads because of the vagaries of the scheduler. You need some sort of pram model guarantees. |
Quote:
|
All times are GMT -8. The time now is 06:07 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project