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
posted
0
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
posted
0
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
posted
0
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
posted
0
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.
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.