A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Quicksort in JavaA quick intro to the Quicksort algorithm in Java, showing an efficient (if not optimal) way to sort. |
On Monday, July 7th 2008 at 07:28 PM By Jesse Bonzo (View Profile) ![]() ![]() ![]() ![]() (Rated 4 with 7 votes) |
||
|
In my opinion, Quicksort is the best algorithm for sorting, when it is implemented correctly. It is the default sorting algorithm in Java itself. Of course, writing it in Java is probably not the best, but it is important to know how it works in case you need to write some variation of it for some other purpose.
This code is a little tricky to understand at first, but go through it slowly. First, we swap the pivot with the last element in the segment of the array being partitioned. Second, we set the marker index to be the front of the segment. Then the array is scanned, checking each element to see if it is less than or equal to the pivot item. If it is, the it is swapped to the front, which is where index points to and index is incremented. At the end of all that, index points to where the pivot should be replaced. If no elements were found to be less than the pivot, then the pivot should be the first element, and like wise, if all the elements were found to be less than the pivot, the pivot should be the last element. The pivot's index is returned so that it can be used in the recursive sort step. Also, the pivot is now in it's final place and does not need to be moved anymore.
This is the actual sorting function that is called recursively. See that since the newPivotIndex points to where the pivot was, and it needs to stay there, the function is only called on the segments from lo to newPivotIndex-1 and newPivotIndex+1 to hi.
|
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsNice your providing real life solutions to real life entities and problems! Thank you very much!
Since you're so smart, I bet you're super hot. :)
we cant read the code
yung steps po
Fantastic! Thank you very much, this helped me a bunch with my assignment here. I still have no clue how to implement this using threads, however, but I guess I'll manage, once I get to know it better. Once again, thank you. ;)
Inplace mergesort is a lot better (faster) and so is randomized quicksort (at least it's never a lot slower) -- in fact, quick sort is not that quick at all. Heapsort and smoothsort can be faster too.
Most of the time you will want to sort your list with inplace mergesort and then move on to something that take advantage of the fact that your list is already partially sorted (like insertion sort or smoothsort) after that.
Nice code but he is not the inventer of the QuickSort. That guy is Antony Hoare.
wala
wala
;khn
Related Tutorials
Related Source Code
Java Job Search