• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about sorting

 
David Wong
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have more than one hundred thousand records need to sort, what is the best algorithm can work at a high performance.
name math english total ranklist
jack 80.00 90.00 170.00 2
tom 95.00 90.00 185.00 1
jerry 50.00 40.00 90.00 3
I want to sort ranklist dynamically,when a new thing was added, then sort it ,but pay attention please, there are one thousand records here at least. Who can find a answer for this, show me the way, thank you in advance and my best wishes.

David
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just want to store your records in a binary tree. Then you can quickly add a new record in the right place without resorting the whole thing.

For example, a TreeSet with an appropriate Comparator.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
You just want to store your records in a binary tree. Then you can quickly add a new record in the right place without resorting the whole thing.

For example, a TreeSet with an appropriate Comparator.


@OP: A small side note:
A binary tree (BT) tells nothing about the order of the structure: a BT is just a structure where each node in it has at 0, 1 or 2 children. A binary search tree (BST) however orders the nodes: "smaller" nodes go into the left subtree and "greater" (or equal) nodes into the right subtree of each node. Now a TreeSet or TreeMap is backed up internally by a red-black tree which is a special kind of BST: one that, while inserting nodes, balances itself so that the tree guarantees lookups, insertions etc. in log(n) time, where n is the total number of nodes in the tree.
[ August 29, 2008: Message edited by: Piet Verdriet ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48935
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More of a beginner's question. I presume you are familiar with the common sort algorithms.
The question is: do you require a stable sort? If yes, then use recursive merge sort, if no use quicksort. Both run in O(nlogn) time

Once you have sorted your collection then a sorted binary tree is probably the best way to maintain sorting, as you have been told; in fact transferring your entire record to a binary sorted tree will sort it in O(nlogn) time anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic