private void mergeCollapse() {
            while (stackSize > 1) {
                int n = stackSize - 2;
                if ((n >= 1 && runLen[n - 1] <= runLen[n] + runLen[n + 1])
                        || (n >= 2 && runLen[n - 2] <= runLen[n] + runLen[n - 1])) {
                    if (runLen[n - 1] < runLen[n + 1])
                        n--;
                } else if (runLen[n] > runLen[n + 1]) {
                    break; // Invariant is established
                }
                mergeAt(n);
            }
        }
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.
indexr-query-opt/src/main/java/org/apache/spark/util/collection/TimSort.java