1

What happened if I sort a Java array with criteria based on its element?

Point[] points = new Point[10];
Arrays.sort(temp, points[0].SLOPE_ORDER);

Will this be a recursive call?

SLOPE_ORDER is a comparator:

public final Comparator<Point> SLOPE_ORDER = new SlopeOrder(); // YOUR DEFINITION HERE
private class SlopeOrder implements Comparator<Point>
{
 public int compare(Point p1, Point p2)
 {
 ...
 }
}
asked Apr 29, 2014 at 22:37
4
  • 2
    what is points[0].SLOPE_ORDER ? Commented Apr 29, 2014 at 22:41
  • Would it make a difference whether the sorting is recursive or not? Every recursion can be written as an iteration. Applying the blackbox principle that means: "Arrays.sort()" is working. IF this question is in the scope of homework like "Apply a recursive sorting algorithm" that answer is for sure not, what is expected. Commented Apr 29, 2014 at 22:42
  • SLOPE_ORDER should be a comparator function. It will be called each time the algorithm needs to compare two elements. Commented Apr 29, 2014 at 22:44
  • The big question is, why do you need to know if it will be a recursive call? Commented Apr 29, 2014 at 22:48

2 Answers 2

4

See here:

Arrays.sort

Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

So the answer is: no, it's not recursive.

answered Apr 29, 2014 at 22:44

Comments

1

Judging by the naming convention and the contract of Array#sort, SLOPE_ORDER is a static final member of the Point class that you are using. Specifically, it's a comparator that can be passed to the sort method.

To answer your question, nothing interesting will happen.

Arrays.sort(temp, points[0].SLOPE_ORDER);

will evaluate points[0].SLOPE_ORDER as the reference to the object and use its value as an argument to perform the sorting without needing to look at points[0] ever again (at least not in order to get SLOPE_ORDER.

If SLOPE_ORDER is both static and final, this can be replaced with

Arrays.sort(temp, Point.SLOPE_ORDER);

the result will be exactly the same and the code is much easier to understand.

answered Apr 29, 2014 at 22:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.