集册 Java实例教程 插入排序的通用方法。

插入排序的通用方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

396
插入排序的通用方法。


//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
展开阅读全文