private static int countRunAndMakeAscending(Object[] a,int lo,int hi,Comparator c){
  assert lo < hi;
  int runHi=lo + 1;
  if (runHi == hi)   return 1;
  if (c.compare(a[runHi++],a[lo]) < 0) {
    while (runHi < hi && c.compare(a[runHi],a[runHi - 1]) < 0)     runHi++;
    reverseRange(a,lo,runHi);
  }
 else {
    while (runHi < hi && c.compare(a[runHi],a[runHi - 1]) >= 0)     runHi++;
  }
  return runHi - lo;
}

private static int countRunAndMakeAscending ( Object [ ] a , int lo , int hi , Comparator c ) { assert lo < hi ; int runHi = lo + 1 ; if ( runHi == hi ) return 1 ; if ( c . compare ( a [ runHi ++ ] , a [ lo ] ) < 0 ) { while ( runHi < hi && c . compare ( a [ runHi ] , a [ runHi - 1 ] ) < 0 ) runHi ++ ; reverseRange ( a , lo , runHi ) ; } else { while ( runHi < hi && c . compare ( a [ runHi ] , a [ runHi - 1 ] ) >= 0 ) runHi ++ ; } return runHi - lo ; }
returns the length of the run beginning at the specified position in the specified array and reverses the run if it is descending ( ensuring that the run will always be ascending when the method returns ) . a run is the longest ascending sequence with : a [ lo ] < = a [ lo + 1 ] < = a [ lo + 2 ] < = . . . or the longest descending sequence with : a [ lo ] > a [ lo + 1 ] > a [ lo + 2 ] > . . . for its intended use in a stable mergesort , the strictness of the definition of " descending " is needed so that the call can safely reverse a descending sequence without violating stability .
