InsertionSorter.java

jmud-0.11/src/util/jgp/algorithm/InsertionSorter.java · 815 B

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

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);
    }
}

Site Stats

Snippets: 1791
Codebases: 571