Java排序工具类

Java排序工具类


package sort;

import java.util.Arrays;
import java.util.List;

/**
 * The class contains util methods
 *
 *
 **/
final class SortUtils {


    /**
     * Helper method for swapping places in array
     * @param array The array which elements we want to swap
     * @param idx index of the first element
     * @param idy index of the second element
     */
    static <T> boolean swap(T[] array, int idx, int idy){
        T swap = array[idx];
        array[idx] = array[idy];
        array[idy] = swap;
        return true;
    }


    /**
     * This method checks if first element is less then the other element
     * @param v first element
     * @param w second element
     * @return true if the first element is less then the second element
     */
    static <T extends Comparable<T>> boolean less(T v, T w) {
        return v.compareTo(w) < 0;
    }


    /**
     * Just print list
     * @param toPrint - a list which should be printed
     */
    static void print(List<?> toPrint){
        toPrint.stream()
                .map(Object::toString)
                .map(str -> str + " ")
                .forEach(System.out::print);

        System.out.println();
    }


    /**
     * Prints an array
     * @param toPrint - the array  which should be printed
     */
    static void print(Object[] toPrint){
        System.out.println(Arrays.toString(toPrint));
    }



    /**
     * Swaps all position from {@param left} to @{@param right} for {@param array}
     * @param array is an array
     * @param left is a left flip border of the array
     * @param right is a right flip border of the array
     */
    static <T extends Comparable<T>> void flip(T[] array, int left, int right) {
        while (left <= right) {
            swap(array, left++ , right--);
        }
    }
}


推荐教程

Java设计模式

创建模式、结构模式、行为模式

Maven教程

Maven是一个项目管理和构建工具

Lucene教程

Lucene是一个开源的基于Java的搜索库

JUnit教程

JUnit是Java编程语言的单元测试框架

Java算法

排序、二叉树、红黑树

Log4j教程

log4j是一种用Java编写的可靠,快速和灵活的日志框架(API)

StringBoot教程

StringBoot实战操作演练

mybatis文档手册

Mybatis基础语法、实例讲解

SpringCloud教程

服务发现、断路器、智能路由

Guava教程

Guava是开源的Java工具库

Elasticsearch教程

一款强大的搜索引擎