This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question regarding Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question regarding Sorting" Watch "Question regarding Sorting" New topic
Author

Question regarding Sorting

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi all,

I was just wondering if people are using Collections.sort() or any one of the sorting techniques like the quciksort, merge sort etc...? And also what is the fastest sorting algorithm?

Thanks.


Be Humble... Be Nice.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Use the Collections.sort(). Also look at the Comparable & Comparator interfaces.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There's no one "fastest" algorithm; there are some special-case sorts you can do in time proportional to the number of items, but most general-purpose fast sorts perform in time proportional to the number of items times the logarithm of that number. Sometimes a fast sort can "accidentally" run much more slowly, if, for example, the data is already mostly sorted. High-quality implementations can avoid this kind of problem, which is why it's always a much better idea to use a library sort than to try to implement your own.

Colletions.sort() is an implementation of mergesort, by the way.


[Jess in Action][AskingGoodQuestions]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

"fastest sorting" has been an academic research topic for 50+ years. As posted above, there is no "fastest" in all cases. Read any book on algorithm analysis, they all have a chapter or two on the topic.

Be careful if you get into large sets to sort. Some generally well behaved algorithms fail badly in some cases. Some do really bad if the set is already sorted, some if the set is already reverse sorted, etc.

Know your data before you pick one.

if your data is essentially randomly ordered, then the Collections.sort() is fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding Sorting
 
Similar Threads
javac not recognized as an internal or external command
How do I include a custom font in a NetBean project?
Multi Level Sorting
Error
Problem with Jar