2018-11-14 01:15:47 +08:00
|
|
|
package Sorts;
|
2018-04-09 17:05:41 +08:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The class contains util methods
|
|
|
|
*
|
|
|
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
2020-10-24 18:23:28 +08:00
|
|
|
*/
|
2018-04-09 17:05:41 +08:00
|
|
|
final class SortUtils {
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2020-07-01 03:44:38 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* This method checks if first element is less than the other element
|
|
|
|
*
|
|
|
|
* @param v first element
|
|
|
|
* @param w second element
|
|
|
|
* @return true if the first element is less than the second element
|
|
|
|
*/
|
|
|
|
static <T extends Comparable<T>> boolean less(T v, T w) {
|
|
|
|
return v.compareTo(w) < 0;
|
|
|
|
}
|
2020-07-01 03:44:38 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* This method checks if first element is greater than the other element
|
|
|
|
*
|
|
|
|
* @param v first element
|
|
|
|
* @param w second element
|
|
|
|
* @return true if the first element is greater than the second element
|
|
|
|
*/
|
|
|
|
static <T extends Comparable<T>> boolean greater(T v, T w) {
|
|
|
|
return v.compareTo(w) > 0;
|
|
|
|
}
|
2018-04-09 17:05:41 +08:00
|
|
|
|
2021-10-13 12:54:55 +08:00
|
|
|
/**
|
|
|
|
* This method checks if first element is greater than or equal the other element
|
|
|
|
*
|
|
|
|
* @param v first element
|
|
|
|
* @param w second element
|
|
|
|
* @return true if the first element is greater than or equal the second element
|
|
|
|
*/
|
|
|
|
static <T extends Comparable<T>> boolean greaterOrEqual(T v, T w) {
|
|
|
|
return v.compareTo(w) >= 0;
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Prints a 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);
|
2018-04-09 17:05:41 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
System.out.println();
|
|
|
|
}
|
2018-04-09 17:05:41 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Prints an array
|
|
|
|
*
|
|
|
|
* @param toPrint - an array which should be printed
|
|
|
|
*/
|
|
|
|
static void print(Object[] toPrint) {
|
|
|
|
System.out.println(Arrays.toString(toPrint));
|
|
|
|
}
|
2018-04-10 17:48:21 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* 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--);
|
2018-04-09 17:05:41 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
}
|
2018-04-09 17:05:41 +08:00
|
|
|
}
|