Thursday, February 16, 2017

MeteredInsertionSorter


class MeteredInsertionSorter<T extends Comparable<T>>
extends MeteredSorter<T> {
  public void maneuver(T[] seq, int from, int to) {
 
    final int step = from < to ? 1 : -1;
    final int stop = to + step;
    int i, jCurr, jPrev;
    T key;

    for (i = from + step; i != stop; i += step) {
      tick();
      key = seq[i];
      for (jCurr = i; jCurr != from; jCurr = jPrev) {
        tick();
        jPrev = jCurr - step;
        if (key.compareTo(seq[jPrev]) >= 0) {
          break;
        }
        seq[jCurr] = seq[jPrev];
      }
      seq[jCurr] = key;
    }
    return;
  }
}

index  -1 0 1 2 3 4 5
seq     
from     
to     
stop               
             
jCurr               
jPrev               
key
 
step
 
meter
0