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.

Recommended Answers

All 5 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.