I was just wondering what would be the best sort algorithm for sorting both a small array and a large one. Or if you can point me to a few efficient ones that can still do both that would be nice.

small <=30

Also is there a cap that java arrays allow. Say an array of 800000?

Thanks in advanced.

6 Years
Discussion Span
Last Post by NaturalSwtHeart

Arrays.sort (in the java.util package) is the way to go in Java. In fact, no matter what language you're working in, if it has a built-in sort function, you should use it rather than writing one yourself. It's easier and also probably faster, since the built-in one is probably highly optimized. (If you're working in a language that doesn't have a built-in sort function, use Quicksort, which, generally speaking, is very efficient.)

There are a lot of sorting algorithms out there, and they do have their strengths and weaknesses, but from a practical standpoint you generally don't care about them. The reason you learn about them in school is because they're a good case study in comparing algorithms, not because you actually need to know them.

To answer your second question: The maximum allowed length of a Java array is Integer.MAX_VALUE, or 2147483647. In practice, the maximum size array you'll be able to create is probably a lot smaller, due to running out of heap memory. You can probably get away with 800000, though.

Edited by JDCyrus: n/a


Thanks, I want looking to use array.sort..

I was looking more into the quick sorts and lg n algorithms.
I'm learning about them now but I'm ask a little fuzzy what's better tho. Plus I'm not using int our strings arrays. More of um idk what you would call items of a certain class..

Idk but I thought that the advanced quick sort might be a good one to try.
Thanks :)


Arrays.sort works fine for user-defined classes. In principle, it's the same as with Strings or numbers, since for the sort to be meaningful it must be possible to compare one instance of the data type in question with another. If your class already implements Comparable<YourClass> (or a wildcard which includes that), then you're good; if not, write a class that implements Comparator<YourClass> (or a wildcard which includes that), which you can find in the java.util package, and pass that to Arrays.sort.

One warning: If you're using Quicksort, know that it's not a stable sort. That means that if two or more items are equal according to the comparison method, they will not necessarily be in the same order relative to each other that they were before the array was sorted. If you were sorting numbers or Strings, you wouldn't care (because two equal numbers or Strings are interchangeable), but if you're using your own data types, you might. Arrays.sort is guaranteed to be stable, which is another advantage of using it.


thanks a bunch :)
I already knew quick sort wasn't stable, but it doesn't matter since there are no duplicates. so that wasn't a big issue for me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.