timsort.js
/**
 * Merges the two runs at stack indices i and i+1.  Run i must be
 * the penultimate or antepenultimate run on the stack.  In other words,
 * i must be equal to stackSize-2 or stackSize-3.
 * 
 * @param {TimSort} tim 
 * @param {Number} i 
 */
Merges the two runs at stack indices i and i+1.  Run i must be
the penultimate or antepenultimate run on the stack.  In other words,
i must be equal to stackSize-2 or stackSize-3.

@param {TimSort} tim
@param {Number} i
Merges the two runs at stack indices i and i + 1 . Run i must be the penultimate or antepenultimate run on the stack . In other words i must be equal to stackSize - 2 or stackSize - 3 . @param { TimSort } tim @param { Number } i
function mergeAt(tim, i){
    let runBase = tim.runBase,
        runLen = tim.runLen,
        arr = tim.arr,
        c = tim.comparator;
    let base1 = runBase[i],
        len1 = runLen[i],
        base2 = runBase[i + 1],
        len2 = runLen[i + 1];
    runLen[i] = len1 + len2;
    if (i === tim.stackSize - 3) {
        runBase[i + 1] = runBase[i + 2];
        runLen[i + 1] = runLen[i + 2];
    }
    tim.stackSize--;
    let k = gallopRight(arr[base2], arr, base1, len1, 0, c);
    base1 += k;
    len1 -= k;
    if (len1 === 0){
        return;
    }
    len2 = gallopLeft(arr[base1 + len1 - 1], arr, base2, len2, len2 - 1, c);
    if (len2 === 0){
        return;
    }
    if (len1 <= len2){
        mergeLo(tim, base1, len1, base2, len2);
    } 
    else {
        mergeHi(tim, base1, len1, base2, len2);
    }
}
function mergeAt ( tim , i ) { let runBase = tim . runBase , runLen = tim . runLen , arr = tim . arr , c = tim . comparator ; let base1 = runBase [ i ] , len1 = runLen [ i ] , base2 = runBase [ i + 1 ] , len2 = runLen [ i + 1 ] ; runLen [ i ] = len1 + len2 ; if ( i === tim . stackSize - 3 ) { runBase [ i + 1 ] = runBase [ i + 2 ] ; runLen [ i + 1 ] = runLen [ i + 2 ] ; } tim . stackSize -- ; let k = gallopRight ( arr [ base2 ] , arr , base1 , len1 , 0 , c ) ; base1 += k ; len1 -= k ; if ( len1 === 0 ) { return ; } len2 = gallopLeft ( arr [ base1 + len1 - 1 ] , arr , base2 , len2 , len2 - 1 , c ) ; if ( len2 === 0 ) { return ; } if ( len1 <= len2 ) { mergeLo ( tim , base1 , len1 , base2 , len2 ) ; } else { mergeHi ( tim , base1 , len1 , base2 , len2 ) ; } }
Merges the two runs at stack indices i and i+1.
Merges the two runs at stack indices i and i + 1 .
// assert stackSize >= 2;
 // assert i >= 0;
 // assert i == stackSize - 2 || i == stackSize - 3;
 //assert len1 > 0 && len2 > 0;
 // assert base1 + len1 == base2;
 /*
     * Record the length of the combined runs; if i is the 3rd-last
     * run now, also slide over the last run (which isn't involved
     * in this merge).  The current run (i+1) goes away in any case.
     */ /*
     * Find where the first element of run2 goes in run1. Prior elements
     * in run1 can be ignored (because they're already in place).
     */ //assert k >= 0;
 /*
     * Find where the last element of run1 goes in run2. Subsequent elements
     * in run2 can be ignored (because they're already in place).
     */ // Merge remaining runs, using tmp array with min(len1, len2) elements
