Thread: for those who also know java please help

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    for those who also know java please help

    can someone help me to translate this code to java

    Code:
    template<typename ItemType>
    void MedianHybridQuickSortImpl(ItemType* array, unsigned f, unsigned l)
    {
        while(f+16 < l)
        {
            ItemType v1 = array[f], v2 = array[l], v3 = array[(f+l)/2];
            ItemType median =
                v1 < v2 ?
                ( v3 < v1 ? v1 : std::min(v2, v3)
                 ) :
                ( v3 < v2 ? v2 : std::min(v1, v3)
                 );
            unsigned m = Partition(array, f, l, median);
            MedianHybridQuickSortImpl(array, f, m);
            f = m+1;
        }
    }
    
    template<typename ItemType>
    void MedianHybridQuickSort(ItemType* array, unsigned size)
    {
        MedianHybridQuickSortImpl(array, 0, size-1);
        InsertionSort(array, size);
    }
    I actually don't understand all the std::min thing and the ? sign

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    std::min() is used for finding minimum values of a pair.
    ? : is a conditional operator similar to if else.
    Syntax is
    (condition)?stts to execute if condn true : stts to execute if condn false.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    For one thing, Java doesn't support the template keyword, so you will need to come up with
    a hierarchy for your ItemType, or use the Comparable interface to free yourself from the type,
    and allow Objects that are Comparable to be correctly handled by the function.
    As for the mysterious ? : characters, Java supports the ternary operator, and i understand it
    even when nested, but would never ever suggest using it obfuscates code which would be
    usually written more clearly with simple if statements. Also the operation performed is not so
    difficult to rewrite yourself. It just picks the middle valued of the three ItemType's named v1,v2,v3.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java for real-time applications
    By zacs7 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-26-2008, 06:34 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. First Java Class at college
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 09-29-2004, 10:38 PM
  4. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  5. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM