Hello, I need to perform a binary search on an array of objects but to do so I need to have them sorted. I've been looking online and no one really has a good explanation of how to conduct a quick sort on an array of objects. If you can direct me to a good tutorial that would be great.
Friends = new Friends is an example of my array.
Pretty much I just want to be able to say myarray.quickSort() and it sorts it. Picking a random piv point. Therefore I can call it w.e needed. I'm just stuck on how I would write it. I know it's a divide and conqueror algo.
Have a look at the Arrays class - it has sort methods for arrays (and binary searches). You just need to implement a compareTo method for your Friends class (you'll find docs and examples in all the usual places).
I think the attachment will help you. I read a book about Java and it talked about this, so this is the code to do it. The program uses recursion to divide the array into two and use the pivot to put the bigger numbers in one side, and the smaller in another, then it does that again with each part until your array is sorted.
To fix your problem with comparable simply override the parent's compareTo() method by naming a method in the child class the same as the parent's method or call the parents method using the super keyword like this.
this may not solve your problem I am only guessing at what is wrong because you did not give me any code to work with.
What do you want to sort them by? As in what properties(e.g. color, name, size, . . .) define your object sort(what do you want to sort them by)? Do you want to create a general sort method that you can enter in a number or string and it will sort the objects by that property?
I'm not sure what it is you're after here, but if you put a compareTo method in the abstract superclass or in each of the derived classes (if they sort differently) then you're good on comparable.
If you need to implement quicksort, it's simple enough in principle, yancouto gave you a very good summary of the basic idea. Break it down to component parts and write and test them before you try to combine the whole thing, that'll save you some headache.