Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

A question about sorting

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
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
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.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic