• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

quick sort algorithm

 
Jeremy King
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic