private void mergeCollapse(){
  while (stackSize > 1) {
    int n=stackSize - 2;
    if (n > 0 && runLen[n - 1] <= runLen[n] + runLen[n + 1]) {
      if (runLen[n - 1] < runLen[n + 1])       n--;
      mergeAt(n);
    }
 else     if (runLen[n] <= runLen[n + 1]) {
      mergeAt(n);
    }
 else {
      break;
    }
  }
}

private void mergeCollapse ( ) { while ( stackSize > 1 ) { int n = stackSize - 2 ; if ( n > 0 && runLen [ n - 1 ] <= runLen [ n ] + runLen [ n + 1 ] ) { if ( runLen [ n - 1 ] < runLen [ n + 1 ] ) n -- ; mergeAt ( n ) ; } else if ( runLen [ n ] <= runLen [ n + 1 ] ) { mergeAt ( n ) ; } else { break ; } } }
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 .
