插入排序的通用方法。
//package com.nowjava; /**来自 N o w J a v a . c o m**/ import java.util.Comparator; public class Main { /** * A generic method. Sorts the input array using an insertion sort and the * input Comparator object. * * @param array * -- the input array of objects to be sorted * @param mComparator * -- the comparator to be used in the insertion sort */ public static <T> void insertionSort(T[] array, Comparator<? super T> mComparator) { int i, j; for (i = 1; i < array.length; i++) { T index = array[i]; // object to be inserted // until the insertion point is found, shift items for (j = i; j > 0 && mComparator.compare(index, array[j - 1]) < 0