IntelliJ Java IDE
The moose likes Performance and the fly likes Sorting algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Sorting algorithm" Watch "Sorting algorithm" New topic
Author

Sorting algorithm

Raji Ram
Ranch Hand

Joined: Aug 22, 2002
Posts: 46
I have an application in which half the data comes from the database and the other half from a message Queue.
The data from the database is sorted whereas that from the queue is not.
I have to combine both the lists and resort them.
The combined list would be really big(more than 10000 rows most of which comes from the sorted sql result)
Any thoughts on which sorting algorithm would be the best in this situation?
Thx in advance.


Regards,<br />Raji.
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Since half the data is already sorted, Insertion Sort should be easy and fairly fast. I'd say verify with Quick Sort too.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Insertion sort using a binary search could work, depending on how the data is stored -- but you might as well just sort the queue data and merge the two sorted lists as in merge sort. That should be simple to implement and perform decently regardless of the way the queue and database are implemented.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You might also just try the sort algorithm implemented by the Collections framework.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Imad Uddin Chishti
Greenhorn

Joined: Oct 13, 2002
Posts: 15
HI!
Collection sort wont resolve the issue in case ur resultset spans over more than 1 column and you want sort to be applied on multiple columns, however in case you want sort to be applied on a single column and is retrieving 1 column only than Collection sort can be used. simply by adding those inside a Vector and applying sort on it, however if no of columns are more than one but sort is to be applied for one column only than create a vector for information that is to be linked to a specific object which is to be used as key and save it with that key object in a Hashtable and hence you can receive a sorted list. however you can also apply binary search algorithm for which you have create your own implementation for the algorithm.


Imad Uddin Chishti<br />Sun Certified JAVA Programmer platform 2<br />Senior Consultant<br />BearingPoint Pakistan
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Imad Uddin Chishti:
Collection sort wont resolve the issue in case ur resultset spans over more than 1 column and you want sort to be applied on multiple columns

That's not quite true - if every row is represented by an object and those objects implement the Comparable interface (or you pass a custom Comparator to the sort method), it should work rather well.
 
 
subject: Sorting algorithm
 
Threads others viewed
How does a PriorityQueue work ?
Java versus Oracle
QuickSort Question
sorting in bean objects
Arrays BinarySearch
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture