timsort.js
/**
 * Examines the stack of runs waiting to be merged and merges adjacent runs
 * until the stack invariants are reestablished:
 *
 *     1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1]
 *     2. runLen[i - 2] > runLen[i - 1]
 *
 * This method is called each time a new run is pushed onto the stack,
 * so the invariants are guaranteed to hold for i < stackSize upon
 * entry to the method.
 * @param {TimSort} tim 
 */
Examines the stack of runs waiting to be merged and merges adjacent runs
until the stack invariants are reestablished.

1.

This method is called each time a new run is pushed onto the stack,
so the invariants are guaranteed to hold for i < stackSize upon
entry to the method.
Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished . 1 . This method is called each time a new run is pushed onto the stack so the invariants are guaranteed to hold for i < stackSize upon entry to the method .
function mergeCollapse(tim){
    let runLen = tim.runLen;
    while (tim.stackSize > 1) {
        let n = tim.stackSize - 2;
        if (n > 0 && runLen[n-1] <= runLen[n] + runLen[n+1]) {
            if (runLen[n - 1] < runLen[n + 1]){
                n--;
            }
            mergeAt(tim, n);
        } else if (runLen[n] <= runLen[n + 1]) {
            mergeAt(tim, n);
        } else {
            break; 
        }
    }
}
function mergeCollapse ( tim ) { let runLen = tim . runLen ; while ( tim . stackSize > 1 ) { let n = tim . stackSize - 2 ; if ( n > 0 && runLen [ n - 1 ] <= runLen [ n ] + runLen [ n + 1 ] ) { if ( runLen [ n - 1 ] < runLen [ n + 1 ] ) { n -- ; } mergeAt ( tim , n ) ; } else if ( runLen [ n ] <= runLen [ n + 1 ] ) { mergeAt ( tim , n ) ; } else { break ; } } }
Examines the stack of runs waiting to be merged and merges adjacent runs
 until the stack invariants are reestablished:
Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished :
// Invariant is established
