Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting algorithm  RSS feed

 
Raji Ram
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also just try the sort algorithm implemented by the Collections framework.
 
Imad Uddin Chishti
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!