Sorting with TreeMap vs. quicksort

I've been sorting lists by throwing them into a TreeMap then retrieving them in order (especially for fairly evenly distributed lists of strings), thereby leveraging its binary tree characteristics.
My belief is that sorting this way takes N log N time, just as quicksort (as in Arrays.sort) does -- because adding items to a TreeMap takes log N time -- without any danger of stack overflows from recursing on a huge list as with quicksort, and fewer method calls which can get expensive.
I'm figuring that even if this way of sorting uses more memory than an in-place sort as in Arrays.sort, at least it won't overflow the stack, and will mostly be eligible for garbage collection anyway.
Could someone correct or confirm my conclusions, reasoning or assumptions? And how well do they apply to Collections.sort's merge sort?

Using a tree guarentees worst case O(n log n) for n
inserts.Amazing, my untrained intuition was correct.
As for stack problems these are unlikely to occur until
logn is at least a few thousand (think about stupidly large values of
n).I regularly need to sort lists of tends of thousands of strings.
.. its [quicksort's] gotcha is that
performance can degrade well below this approaching
O(n^2) (might be higher. I cant remember).Yes, O(n^2) for adversarial data. Does mergesort require a shallower recursion than quicksort does?
Since temporary use of the heap is rarely a concern for me, and datasets are very large, it sounds like mergesort and the TreeMap are viable contenders for preferred sorting methods. But I'm still apprehensive about mergesort's use of recursion.
Thank you, I gave you 5 duke dollars and matei 2.

Similar Messages

  • A problem with treemap

    In a file, I have 400, 000 lines of data. There are around 16000 keys. Each key can have multiple values.
    eg:
    Key - Var1 - Var2
    5 - 132.1 - 0.678
    5 - 128.5 - 0.523
    2 - 110.0 - 0.253
    6 - 20.0 - 0.245
    I sorted using TreeMap. However, only 12463 keys gets added to TreeMap. I don't know what's wrong . If someone can help me out with this problem, that would be great!!!
    -Karthik

    So if you want to use the standard HashMap you have to deal with multiple keys yourself.
    One way is to first check if the key's already in the map. If it isn't you store the key/item pair as you do now. If the key's already in the map you switch to a collection of items using a collection, for example a LinkedList. The end result is that each key is uniqueande there are two types of items; single items and collections of items.

  • Sorting the TreeMap

    I am trying to sort a TreeMap based on Values like this:-
    public class SortedValueTest
    public static void main( String[] args )
    Map map = new TreeMap();
    map.put( "field1", new Integer( 0 ) );
    map.put( "field2", new Integer( 2 ) );
    map.put( "field3", new Integer( 2 ) );
    map.put( "field4", new Integer( 0 ) );
    Set set = new TreeSet(
    new Comparator()
    public int compare( Object o1, Object o2 )
    Map.Entry e1 = (Map.Entry)o1;
    Map.Entry e2 = (Map.Entry)o2;
    Integer d1 = (Integer)e1.getValue();
    Integer d2 = (Integer)e2.getValue();
    return d1.compareTo( d2 );
    set.addAll( map.entrySet() );
    for ( Iterator iter = set.iterator(); iter.hasNext(); )
    Map.Entry entry = (Map.Entry)iter.next();
    System.out.println( entry.getKey() + " = " + entry.getValue() );
    The output obtained is like this:-
    field1 = 0
    field2 = 2
    But the required output should not omit the duplicate items.
    So the above solution based on TreeSet does not seems to be applicable.
    Is there any alternative way to solve this problem?
    Thanks in Advance.

    SOLVED!!!
    import java.io.IOException;
    import java.util.*;
    * Created by IntelliJ IDEA.
    * User: ponmanadiyilv
    * Date: 06-Mar-2006
    * Time: 11:46:29
    * To change this template use File | Settings | File Templates.
    public class GoodSort {
    public static void main(String[] args) throws Exception {
    Map m = new HashMap();
    m.put ("field1", new Integer(12));
    m.put ("field2", new Integer(2));
    m.put ("field3", new Integer(30));
    m.put ("field4", new Integer(5555));
    m.put ("field5", new Integer(0));
    m.put ("field6", new Integer(5555));
    ArrayList outputList = sortMap(m);
    int count = 0;
    count = outputList.size();
    while(count > 0) {
    Map.Entry entry = (Map.Entry) outputList.get(--count);
    System.out.print("Key:" + entry.getKey());
    System.out.println("\tValue:" + entry.getValue());
    * This method will use Arrays.sort for sorting Map
    * @param map
    * @return outputList of Map.Entries
    public static ArrayList sortMap(Map map) {
    ArrayList outputList = null;
    int count = 0;
    Set set = null;
    Map.Entry[] entries = null;
    // Logic:
    // get a set from Map
    // Build a Map.Entry[] from set
    // Sort the list using Arrays.sort
    // Add the sorted Map.Entries into arrayList and return
    set = (Set) map.entrySet();
    Iterator iterator = set.iterator();
    entries = new Map.Entry[set.size()];
    while(iterator.hasNext()) {
    entries[count++] = (Map.Entry) iterator.next();
    // Sort the entries with your own comparator for the values:
    Arrays.sort(entries, new Comparator() {
    public int compareTo(Object lhs, Object rhs) {
    Map.Entry le = (Map.Entry)lhs;
    Map.Entry re = (Map.Entry)rhs;
    return ((Comparable)le.getValue()).compareTo((Comparable)re.getValue());
    public int compare(Object lhs, Object rhs) {
    Map.Entry le = (Map.Entry)lhs;
    Map.Entry re = (Map.Entry)rhs;
    return ((Comparable)le.getValue()).compareTo((Comparable)re.getValue());
    outputList = new ArrayList();
    for(int i = 0; i < entries.length; i++) {
    outputList.add(entries);
    return outputList;
    }//End of sortMap

  • Problem with TreeMap

    I had a problem that I believe was similar to this posting:
    http://forum.java.sun.com/thread.jsp?forum=24&thread=200558
    although mine involves a TreeMap. I am adding approximately 250 mappings to a TreeMap, where the key is a String. Each key is unique, and, just to be sure, I analyzed the hashCode of each key, and they were also unique.
    After adding all of the mappings, however, the total size of the TreeMap is only 40! Somewhere, app. 210 mappings were overwritten. I added some debug code and found that after I added the 10th element, the size of the TreeMap did grow. Then the TreeMap would grow only on every 3rd mapping that I would add.
    The above-referenced posting mentioned that the rehashing of the map might cause it to overwrite existing values, so it made sense to set the initial capacity high enough to ensure that no rehashing occurred. Unfortunately, that posting dealt with a HashMap, which allows you to set the initial capacity. TreeMaps do not. In order to mimic this behavior, I added all of the mappings into a HashMap with an initial capacity of 250. When the HashMap was loaded, I created the TreeMap (with a unique Comparator), and then used the putAll() method of TreeMap to put the entire HashMap into the TreeMap. Voila! All 250 mappings were there.
    Nonetheless, this seems to be a very strange bug with TreeMap. Does anyone know why this happened?

    Sorry. Figured out the problem. It was all my fault (based on the comparator I was using, which caused the keys to be overwritten). how dare I accuse Sun of deploying buggy software. :)

  • A simple question about wrong sorting with multiple sort columns in Excel 2010

    Hi, everyone! I have encountered a problem that I don't know how to explain.
    So I post it here because I don't know if there is another more relevant forum...
    I have a data sheet with the students' scores for the course. 
    All the data were generated with the randbetween function,
    and pasted with the values.
    To rank the students by their performance,
    I did the sort with the column "total score" as the first sort-column
    and "final term" as the second.
    The weird thing to me is that the order of the data seems problematic.
    That is, all the rows are sorted correctly with the first sort-column.
    But for the rows with the same values of the first sort-column,
    there are some rows whose values of the second sort-column are out of order.
    (please look at the data file at
    www_dot_kuaipan_dot_cn/file/id_67204268108546068_dot_htm
    Please change the "_dot_" to the real literal dot.
    Especially the rows with 56.7 as the first sort-column value
    and some other values near the tail of the list.)
    I tried to manually input and sort the same values of both columns
    in a near-by region. The result was correct.
    When some friend copied all the data to paste in Notepad,
    and reload them in excel. The problem disappears.
    Some friend also tried to wrap a round function at the values of the 1st sort-column,
    the sorting order became correct!
    But they could not explain why either.
    How confusing! I even tried to swap the first and secod sort-column;
    the output was right.
    All the data were generated by randbetween function and pasted with values.
    Where could all the special characters, if any, come?
    Can anyone give me an explanation? I would be very grateful.
    Thanks in advance!

    Re:  Sort is not in proper order
    Sounds as if the data includes spaces or hidden characters that are affecting the sort.
    That is indicated by the fact that manually entering the data resolves the problem.
    Note:  You can use a public file storage website to hold your file and add the link to it in your post.
    Jim Cone
    Portland, Oregon USA
    Special Sort excel add-in (30+ ways to sort) - 3 week no obligation trial
    https://jumpshare.com/b/O5FC6LaBQ6U3UPXjOmX2

  • Sorting with nulls last on the fact column

    anybody tell me how to do the sorting with nulls last in the template? i can't change anything in data model or anywhere other than template.
    i did try to put the descending order,but null values are coming in between positive and negative values.
    <?sort:FACT_Column_Name;'decending';data-type='number'?>
    could anyone please tell me how can i do that in template?
    Thanks in advance.
    Edited by: user12255470 on Apr 12, 2012 5:29 PM

    Hi to all,
    i've solved, is very simple using decode inside the select:
    SELECT
    ID,
    FK_HTMLDB_ITEM, DECODE(FK_HTMLDB_ITEM,1,HTMLDB_ITEM.DATE_POPUP(3,rownum,VAL,'dd-mon-yyyy'),2,HTMLDB_ITEM.TEXT(3,VAL)) VALORE
    FROM TESTID
    my table is:
    CREATE TABLE "DEV"."TESTID"
    (     "ID" NUMBER NOT NULL ENABLE,
         "FK_HTMLDB_ITEM" NUMBER,
         "VAL" VARCHAR2(4000 BYTE))
    Bye Bye

  • Dynamic SORT with multiple components

    Hi all,
    I want to sort an internal table in this way:
    SORT itab BY (component).
    This is not difficult if "component" contains the name of ONE column of itab. But - and here is my problem - how can I sort with multiple components?
    Like this:
    SORT itab BY (comp_1) (comp_2) ... (comp_N).
    The number of components (N) shall be flexible. It is not possible to concatenate the components into one string-variable. This causes an error ITAB_ILLEGAL_COMPONENT.
    Brillant would be a solution which also contains the sort-direction (ASCENDING or DESCENDING for each component like this:
    SORT itab BY (comp_1) (dir_1)
                 (comp_2) (dir_2)
                 (comp_N) (dir_N).
    Is there a way to do so?
    Thanks for all hints!
    Mathias

    Hi Matahias ...
    From ABAP help...
    SORT itab.
    Extras:
    1. ... BY f1 f2 ... fn
    2. ... ASCENDING
    3. ... DESCENDING
    4. ... AS TEXT
    5. ... STABLE
    The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Field symbols not allowed as sort criterion.
    Effect
    The entries in the internal table are sorted in ascending order using the key from the table definition (DATA, TYPES).
    Addition 1
    ... BY f1 f2 ... fn
    Effect
    Uses the sort key defined by the sub-fields f1, f2, ..., fn of the table itab instead of the table key. The fields can be of any type; even number fields and tables are allowed.
    You can also specify the sort fields dynamically in the form (name). If name is blank at run time, the sort field is ignored. If itab is a table with a header line, you can also use a field symbol pointing to the header line of itab as a dynamic sort criterion. A field symbol that is not assigned is ignored. If a field symbol is assigned, but does not point to the header line of the internal table, a runtime error occurs.
    If the line type of the internal table contains object reference variables as components, or the entire line type is a reference variable, you can use the attributes of the object to which a reference is pointing in a line as sort criteria (see Attributes of Objects as the Key of an Internal Table
    You can address the entire line of an internal table as the key using the pseudocomponent TABLE_LINE. This is particularly relevant for tables with a non-structured line type when you want to address the whole line as the key of the table (see also Pseudocomponent TABLE_LINE With Internal Tables).
    If you use one of the additions 2 to 5 before BY, it applies to all fields of the sort key by default. You can also specify these additions after each individual sort field f1, f2, ..., fn. For each key field, this defines an individual sort rule which overrides the default.
    Cheers
    Preetham

  • LOV sorting with upper and lower case

    In a dynamic table, I have the following values:
    aaxyz
    ABC
    Abd
    ACE
    Adf
    AEG
    But when I create the LOV, it sorts
    ABC
    ACE
    AEG
    Abd
    Adf
    aaxyz
    Is there any way to sort a LOV as if case does doesn't matter?

    Except the values in the dynamic table are sometimes acronyms which should be upper case and sometimes not.
    For example:
    eDiscovery
    False Alarm
    FBI
    Federal Department
    where the list sorts:
    FBI
    False Alarm
    Federal Department
    eDiscovery
    The issue is that the list of values is large (over 100 items) and sometimes the item the user is interested in is sorted below all the initial upper case letter (in the case of the eDiscovery element). It needs to sort with the "E" but not be upper case.
    Thanx!

  • DataFormWebPart: Sorting with Last modified bug

    I have created a DataFormWebPart with SPD2013. In my DataFormWebPart, I sort the table by Modifed column. The column is display with follow format:
    <xsl:value-of select="ddwrt:FormatDate(string(@Modified), 2057, 5)"/>
    However, when sorting with descending order. The time at 12:xx is at higher proirity:
    24/04/2015 12:44
    24/04/2015 12:25
    24/04/2015 17:18
    24/04/2015 16:55
    24/04/2015 15:52
    Why is that? Can we fix it? Thank you.

    Thank you for your reply. I have done more test. When I press F5 to refresh the page, the sorting order is correct. However when click on the column header to sort the order is incorrect.
    There are only two sentense start with <xsl:sort
    <xsl:sort select="*[name() = $dvt_sortfield] | @*[name() = $dvt_sortfield] | text()[name(ancestor::*[1]) = $dvt_sortfield]" order="{$dvt_sortdir}" data-type="{$dvt_sorttype}" />
    <xsl:sort select="ddwrt:FormatDate(string(@Modified),2057,5)" order="descending" />
    and here is part of column with wrong order when clicking the header:
    <xsl:call-template name="dvt.headerfield" ddwrt:atomic="1" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
     <xsl:with-param name="fieldname">@Modified</xsl:with-param>
     <xsl:with-param name="fieldtitle">修改時間</xsl:with-param>
     <xsl:with-param name="displayname">修改時間</xsl:with-param>
     <xsl:with-param name="sortable">1</xsl:with-param>
     <xsl:with-param name="fieldtype">x:dateTime</xsl:with-param>
    </xsl:call-template>
    Here is how the sorting looks like:
    24/04/2015 15:52
    24/04/2015 16:55
    24/04/2015 12:44
    27/04/2015 13:49
    27/04/2015 13:53

  • Sorting with Diacritic characters

    Hi,
    While implementing sorting in Endeca search we came across a scenario where the sorting should include diacritic characters. Taking the example of Polish, there are characters like *"A"* and *"Ą"* starting for the product names. When we sort with the product name (A-Z sort), we had seen that the records starting with *"Ą"* are retrieved at the end. In fact the expected behavior being it should follow after the names starting with *"A"*. Please let me know if anyone has come across similar behavior and the fix made for it.
    The endeca version being used is 6.1.3 and the language used is Polish.

    You need to add --lang pl-u-co-standard to the dgidx and dgraph components (in ./config/script/AppConfig.xml).  By default Endeca sorts using endeca collation which "sorts text with lower case before upper case and does not account for character accents and punctuation."  Standard collation sorts data "according to the International Components for Unicode (ICU) standard for the language you specify".  See http://docs.oracle.com/cd/E35641_01/MDEX.621/pdf/AdvDevGuide.pdf , Chapter "Using Internationalized Data" for further details. 
    If by 6.1.3 you mean MDEX 6.1.3 (as opposed to Platform Services) I'm not sure this sorting was available then, you would need to check the chapter listed above in the MDEX 6.1.3 documentation.

  • My computer is all out of sort with the color and it is in like a negitative effect that i cannot seem to fix. How do i fix it?

    my computer is all out of sort with the color and it is in like a negitative effect that i cannot seem to fix. How do i fix it?

    Try System Preferences>Universal Access>Seeing>Display.  See if White on Black' has been selected.
    If so, select 'Black on White'.
    Ciao.
    Message was edited by: OGELTHORPE

  • Sorting with BINS

    Hello people,
    My problem is the following:
    I have 2 columns: C1 and C2.
    The C1 I filter and sort with case.
    The C2 I didn't filter but I use BINS... like, if C2 is something group this in "Others"
    Now, I need to sort this C2, but is not too simple like C1. I'll try to show why:
    TEXT 1 ---------------------------------------
    C1 C2
    C1 C2
    C1 C2
    C1 C2
    TEXT 2 ---------------------------------------
    C1 C2
    C1 C2
    C1 C2
    When I told that I sort C1 with case, is simple, because C1 is not depending to TEXT 1 or TEXT 2.
    C2 is different because depends to TEXT 1 and TEXT 2, and besides this when I sort, the group "Others" appears more than 1 time like as the attributes are:
    If a group cat, dog and bird in "Others" and sort this, didn't sort the Others one time, appears Others 3 times (Trying to sort cat, dog and bird inside Others).
    Someone has a idea of what I can do with this problem ?
    Tks in advance.

    I have this attributes :
    Manifest, Motive1, Motive2
    Values for Manifest: Satisfaction, Reclamation, Idea, ...
    Values for Motive1: Package, Product, Marketing, ...
    Values for Motive2: Mould, Melt, Staning, General, Perfomance, Composition, ...
    Before BIN, i have:
    Manifest: Reclamation
    Motive1: Package Motive2: Perfomance
    Motive1: Package Motive2: Composition
    Motive1: Package Motive2: General
    Motive1: Product Motive2: Mould
    Motive1: Marketing Motive2: General
    Manifest: Satisfaction
    Motive1: Package Motive2: Composition
    Motive1: Package Motive2: Perfomance
    Motive1: Product Motive2: Melt
    After BIN, i have: -- Grouping what I need.
    Manifest: Reclamation
    Motive1: Package Motive2: Perfomance
    Motive1: Package Motive2: Others
    Motive1: Product Motive2: Mould
    Motive1: Marketing Motive2: General
    Manifest: Satisfaction
    Motive1: Package Motive2: General
    Motive1: Product Motive2: Melt
    Now, I have to sort this pivot, but I don't know why. When I try to sort (like perfomance 1 and others 2 marketing 3 product 4 product melt 1 package general 1) I got this:
    Manifest: Reclamation
    Motive1: Package Motive2: Perfomance
    Motive1: Package Motive2: Others
    Motive1: Package Motive2: Others
    Motive1: Marketing Motive2: General
    Motive1: Product Motive2: Mould
    Manifest: Satisfaction
    Motive1: Product Motive2: Melt
    Motive1: Package Motive2: General
    Motive1: Package Motive2: General
    I don't know why, but the BIN didn't sort, they sort ''thinking'' in the things inside the BIN.
    I need to sort this and if possible, without case (to expensive to maintenance..)
    If still unclear, tell pls.
    Tks !

  • Sorting with Name

    hi
    i have procedure with sorting technic..
    for sorting i have used code like this
    ......select ...... .. select ......  rownum rn from table_name
                             where cond...
             where pagenumber ......  pagesize........
    but now i want display result according to sorting,
    with alphabetic sorting.
    means if a user want to search name like start with name "M"
    i want to display result with starting letter is "M" as follows...
    and also user want to move <<next> <<last>> <<first>> <previouse>>for this any doc's or sample example or any ideas
    Edited by: OraclePLSQL on Dec 30, 2010 5:31 PM

    Hi,
    ROWNUM shows the order in which a row was received.
    If the source of the data is a table, then the order is undefined, and ROWNUM is arbitrary.
    If the source of the data is a sub-query or a view, then ROWNUM reflects the ORDER BY clause of that sub-query or view. If there is no ORDER BY clause, then ROWNUM is arbitrarty.
    If you want a Pagination Query , where you each page has a given size (it looks like 3 in your example), and you want to display only one given page (page #2 in your example), then you can use ROWNUM, but assign ROWNUM after the data is ordered.
    So you could modify the query you posted like this:
    SELECT * FROM
       SELECT ENAME,    EMPNO,    SAL
       ,      ROWNUM   AS RN               -- ROWNUM is meaningful here (source is ordered)
       FROM
         SELECT    ENAME, EMPNO, SAL          -- No ROWNUM here (source is not ordered)
         FROM      EMP
    --   WHERE     ename     LIKE 'M%'          -- Any filtering goes here
         ORDER BY  ename
       WHERE ROWNUM <((2*3)+1)
    WHERE RN >= (((2-1)*3)+1)
    ORDER BY  rn
    ;Remember to use an ORDER BY clause in the main query, if you want the output sorted.
    Output (using scott.emp):
    ENAME           EMPNO        SAL         RN
    CLARK            7782       2450          4
    FORD             7902       3000          5
    JAMES            7900        950          6This is the 2nd page; ADAMS, ALLEN and BLAKE would be on page 1.
    I think the analytic ROW_NUMBER function is better than ROWNUM for this kind of thing.
    Also, consider using variables instead of the "magic numbers" 2 and 3 in your WHERE clauses.
    The following query produces the same results as the query above:
    -- How many rows should appear on each page (except maybe the last page)?
    VARIABLE  page_size     NUMBER
    EXEC  :page_size := 3;
    -- Which page do we want to display in this query?
    VARIABLE  page_num     NUMBER
    EXEC  :page_num  := 2;
    WITH     got_rn     AS
         SELECT     ename, empno, sal
         ,     ROW_NUMBER () OVER (ORDER BY ename)     AS rn
         FROM     emp
    --     WHERE     ename     LIKE 'M%'     -- Any filtering goes here
    SELECT       *
    FROM       got_rn
    WHERE       CEIL (rn / :page_size)     = :page_num
    ORDER BY  rn
    ;

  • Sorting large amounts of data with treemap

    Hello. Im doing a project where I have to sort a large amount of data. The data is formed by a unique number and a location (a string).
    Something like this
    NUMBER .... CITY
    1000123 BOSTON
    1045333 HOUSTON
    5234222 PARIS
    2343345 PARIS
    6234332 SEATTLE
    I have to sort the data by location and then by unique number...
    I was using the TreeMap to do this : I used the location string as a key - since I wanted to sort the data by that field - but, because the location string is not unique, at the moment to insert the data on the TreeMap, it overwrites the object with the same location string, saving only the last one that was inserted.
    Is there any Collection that implements sorting in the way that I need it?... or if there isnt such thing... is there any collection that supports a duplicated key object???
    Thanks for your time!
    Regards
    Cesar

    ... or use a SortedSet for the list of numbers (as the associated value for
    the location key). Something like this:voidAddTuple(String location, Integer number) {
       SortedSet numbers= set.get(location);
       if (numbers == null)
          set.put(location, numbers= new TreeSet());
       numbers.put(number);
    }kind regards,
    Jos

  • Sort a treemap/hashmap by values

    I'm writing a really simple program that creates a treemap and writes it line by line to a text file. I want it to be ordered by the values asociated with the keys, not by the keys themselves.
    The map simply has strings for keys, the value with each one is the number of times that string appears in the input text file.
    I've looked at the constructor treemap(Comparator c), but didn't really see how I could use it. Is there a simple way to get the text file sorted by values, not the keys?
    Thanks!

    Do you really need a SortedMap to do this? Is there any point when you really need the data sorted by the String keys?
    Probably easier, once you have the collection of String/frequency pairs, just to dump that into a List and sort the List using a comparator that looks at the frequency primarily.

Maybe you are looking for