src/main/java/Smaller.java
/**
     * Optimised count of ints smaller than me to the right.
     *
     * This was nicked from a similar problem in javascript (without comments).
     * It uses a custom tree sort.
     * I had tried to use TreeSet and TreeMap, which were not efficient enough,
     * but didn't think of creating my own tree!
     * Strangely elegant in its simplicity, once you take the plunge!
     * Added comments to explain (as much as possible) what it is doing.
     *
     * @param unsorted array of ints
     * @return array of counts of smaller ints to the right ...
     */
Optimised count of ints smaller than me to the right.
This was nicked from a similar problem in javascript (without comments).
It uses a custom tree sort.
I had tried to use TreeSet and TreeMap, which were not efficient enough,
but didn't think of creating my own tree.
Strangely elegant in its simplicity, once you take the plunge.
Added comments to explain (as much as possible) what it is doing.

@param unsorted array of ints
@return array of counts of smaller ints to the right
Optimised count of ints smaller than me to the right . This was nicked from a similar problem in javascript ( without comments ) . It uses a custom tree sort . I had tried to use TreeSet and TreeMap which were not efficient enough but didn ' t think of creating my own tree . Strangely elegant in its simplicity once you take the plunge . Added comments to explain ( as much as possible ) what it is doing . @param unsorted array of ints @return array of counts of smaller ints to the right
public static int[] smaller(int[] unsorted) {
        int[] result = new int[unsorted.length];
        Smaller root = null;
        for (int index = unsorted.length - 1; index >= 0; index--) {
            root = insert(root, unsorted[index], result, 0, index);
        }
        return result;
    }
public static int [ ] smaller ( int [ ] unsorted ) { int [ ] result = new int [ unsorted . length ] ; Smaller root = null ; for ( int index = unsorted . length - 1 ; index >= 0 ; index -- ) { root = insert ( root , unsorted [ index ] , result , 0 , index ) ; } return result ; }
Optimised count of ints smaller than me to the right.
Optimised count of ints smaller than me to the right .
// System.out.println(Arrays.toString(unsorted) + " ===>"); // System.out.println(Arrays.toString(result) + " <===");