All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.SortUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import java.util.Comparator;
import java.util.List;

/**
 * 数据排序工具
 * @author wangcheng
 * 2021/8/11
 **/
public class SortUtil {

    /**
     * 冒泡排序
     * @param list 数据
     * @param comparator 对比类
     * @param  数据类
     */
    public static  void bubble(List list, Comparator comparator) {
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.size() - 1; j++) {
                if (comparator.compare(list.get(j), list.get(j + 1)) > 0) {
                    //数据位置交换
                    SortUtil.exchange(list, j, j + 1);
                }
            }
        }
    }

    /**
     * 插入排序
     * @param list 数据
     * @param comparator 对比类
     * @param  数据类
     */
    public static  void insert(List list, Comparator comparator) {
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (comparator.compare(list.get(i), list.get(j)) > 0) {
                    //数据位置交换
                    SortUtil.exchange(list, i, j);
                }
            }
        }
    }

    /**
     * 选择排序
     * @param list 数据
     * @param comparator 对比类
     * @param  数据类
     */
    public static  void choose(List list, Comparator comparator) {
        for (int i = 0; i < list.size(); i++) {
            int index = i;
            for (int j = i + 1; j < list.size(); j++) {
                if (comparator.compare(list.get(index), list.get(j)) > 0) {
                    index = j;
                }
            }
            if (index != i) {
                //数据位置交换
                SortUtil.exchange(list, i, index);
            }
        }
    }

    /**
     * 希尔排序
     * @param list 数据
     * @param comparator 对比类
     * @param  数据类
     */
    public static  void shell(List list, Comparator comparator) {
        int length = list.size();
        for (int gap = length / 2; gap >= 1; gap /= 2) {
            for (int i = gap; i < length; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (comparator.compare(list.get(j), list.get(j + gap)) > 0) {
                        //数据位置交换
                        SortUtil.exchange(list, j, j + gap);
                    }
                }
            }
        }
    }

    /**
     * 快速排序
     * @param list 数据
     * @param comparator 对比类
     * @param startIndex 开始索引
     * @param endIndex 结束索引
     * @param  数据类
     */
    public static  void fast(List list, Comparator comparator, Integer startIndex, Integer endIndex) {
        int i = startIndex, j = endIndex;
        if (i >= j) {
            return;
        }
        T index = list.get(i);
        while (i < j) {
            while (i < j && comparator.compare(index, list.get(j)) <= 0) {
                j--;
            }
            if (i < j) {
                list.set(i++, list.get(j));
            }
            while (i < j && comparator.compare(index, list.get(i)) >= 0) {
                i++;
            }
            if (i < j) {
                list.set(j--, list.get(i));
            }
        }
        list.set(i, index);
        if (i > 0) {
            SortUtil.fast(list, comparator, startIndex, i - 1);
        }
        if (j < endIndex) {
            SortUtil.fast(list, comparator, j + 1, endIndex);
        }
    }

    /**
     * 数据交换
     * @param list 数据列表
     * @param index1 数据索引1
     * @param index2 数据索引2
     * @param  类型
     */
    private static  void exchange(List list, int index1, int index2) {
        //交换数据缓冲区
        T exchangeData = list.get(index1);
        list.set(index1, list.get(index2));
        list.set(index2, exchangeData);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy