jmud-0.11/
jmud-0.11/bin/
jmud-0.11/doc/
jmud-0.11/rec/
jmud-0.11/rec/mun/
jmud-0.11/rec/mun/grecia/
jmud-0.11/rec/mun/gunnar/
jmud-0.11/rec/qua/
jmud-0.11/src/bool/
jmud-0.11/src/clone/
jmud-0.11/src/integer/
jmud-0.11/src/misc/
jmud-0.11/src/string/
jmud-0.11/src/util/bit/
jmud-0.11/src/util/color/
jmud-0.11/src/util/file/
jmud-0.11/src/util/jgp/adaptor/
jmud-0.11/src/util/jgp/algorithm/
jmud-0.11/src/util/jgp/container/
jmud-0.11/src/util/jgp/functor/
jmud-0.11/src/util/jgp/interfaces/
jmud-0.11/src/util/jgp/predicate/
jmud-0.11/src/util/log/
jmud-0.11/src/util/state/
jmud-0.11/trash/
package util.jgp.algorithm;

import util.jgp.interfaces.RandomAccessable;
import util.jgp.predicate.BinaryPredicate;

public class InsertionSorter {

    private RandomAccessable vec      = null;
    private BinaryPredicate  lessThan = null;

    public InsertionSorter(RandomAccessable v, BinaryPredicate less) {
	vec      = v;
	lessThan = less;
    }

    public void sort(int first, int last) {
	for (int i = first; i <= last; ++i) {
	    Object obj = vec.getElementAt(i);

	    int j;
	    for (j = first; j < i; ++j)
		if (lessThan.execute(obj, vec.getElementAt(j))) {

		    int from = i - 1;
		    int to   = i;
		    while (to > j)
			vec.setElementAt(to--, vec.getElementAt(from--));

		    break;
		}

	    vec.setElementAt(j, obj);
	}
    }

    public void sort() {
	sort(0, vec.getSize() - 1);
    }
}