• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Question regarding Sorting

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the Collections.sort(). Also look at the Comparable & Comparator interfaces.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic