wood burning stoves
The moose likes Beginning Java and the fly likes quick sort algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "quick sort algorithm" Watch "quick sort algorithm" New topic

quick sort algorithm

Jeremy King

Joined: May 27, 2008
Posts: 5
I have this problem to do and am having trouble with it. We were told that we should implement the following methods,
public static void sort(Comparable[] a, int begin, int end)
private static int split(Comparable[] a, int begin, int end)
I'm not quite sure where to start.

[edit]Added code tags. CR[/edit]
[ June 06, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46424
Welcome to JavaRanch.

Please use code tags (button below the message window); I have added them for you so you can see how much better quoted code looks.

You need to read about the Comparable interface. Declaring Comparable[] means that every type of your array implements the Comparable interface, and it therefore has a compareTo() method. If you pass an object of the appropriate type to compareTo, you get an int result which is negative if the first object is "less than" the other, or positive if it is "more than" or 0 if they are "the same." Do your programming in little bits, and see that it works before trying the next bit.

You are using String[] arrays; of course Strings implement the Comparable interface.

BTW: It's "the quick brown fox jumps over a lazy dog." But you can have two "the"s if you wish.

You can use that result ( < 0 or == 0 or > 0) to tell which pairs need to be swapped. If that proves awkward, start off with an int[] and use array[i] < array[j], then try array[i] - array[j] < 0, then you replace the int[] array with a Comparable[] array.
I agree. Here's the link: http://aspose.com/file-tools
subject: quick sort algorithm
It's not a secret anymore!