Update SortUtils (#4139)

This commit is contained in:
duyuanch 2023-04-02 23:09:51 +08:00 committed by GitHub
parent acfa2890a4
commit 805f09850c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 67 deletions

View File

@ -54,7 +54,8 @@ class CombSort implements SortAlgorithm {
for (int i = 0; i < size - gap; i++) { for (int i = 0; i < size - gap; i++) {
if (less(arr[i + gap], arr[i])) { if (less(arr[i + gap], arr[i])) {
// Swap arr[i] and arr[i+gap] // Swap arr[i] and arr[i+gap]
swapped = swap(arr, i, i + gap); swap(arr, i, i + gap);
swapped = true;
} }
} }
} }

View File

@ -2,121 +2,118 @@ package com.thealgorithms.sorts;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/**
* The class contains util methods
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*/
final class SortUtils { final class SortUtils {
/** /**
* Helper method for swapping places in array * Swaps two elements at the given positions in an array.
* *
* @param array The array which elements we want to swap * @param array the array in which to swap elements
* @param idx index of the first element * @param i the index of the first element to swap
* @param idy index of the second element * @param j the index of the second element to swap
* @param <T> the type of elements in the array
*/ */
static <T> boolean swap(T[] array, int idx, int idy) { public static <T> void swap(T[] array, int i, int j) {
T swap = array[idx]; T temp = array[i];
array[idx] = array[idy]; array[i] = array[j];
array[idy] = swap; array[j] = temp;
return true;
} }
/** /**
* This method checks if first element is less than the other element * Compares two elements to see if the first is less than the second.
* *
* @param v first element * @param firstElement the first element to compare
* @param w second element * @param secondElement the second element to compare
* @return true if the first element is less than the second element * @return true if the first element is less than the second, false otherwise
*/ */
static <T extends Comparable<T>> boolean less(T v, T w) { public static <T extends Comparable<T>> boolean less(T firstElement, T secondElement) {
return v.compareTo(w) < 0; return firstElement.compareTo(secondElement) < 0;
} }
/** /**
* This method checks if first element is greater than the other element * Compares two elements to see if the first is greater than the second.
* *
* @param v first element * @param firstElement the first element to compare
* @param w second element * @param secondElement the second element to compare
* @return true if the first element is greater than the second element * @return true if the first element is greater than the second, false otherwise
*/ */
static <T extends Comparable<T>> boolean greater(T v, T w) { public static <T extends Comparable<T>> boolean greater(T firstElement, T secondElement) {
return v.compareTo(w) > 0; return firstElement.compareTo(secondElement) > 0;
} }
/** /**
* This method checks if first element is greater than or equal the other * Compares two elements to see if the first is greater than or equal to the second.
* element
* *
* @param v first element * @param firstElement the first element to compare
* @param w second element * @param secondElement the second element to compare
* @return true if the first element is greater than or equal the second * @return true if the first element is greater than or equal to the second, false otherwise
* element
*/ */
static <T extends Comparable<T>> boolean greaterOrEqual(T v, T w) { static <T extends Comparable<T>> boolean greaterOrEqual(T firstElement, T secondElement) {
return v.compareTo(w) >= 0; return firstElement.compareTo(secondElement) >= 0;
} }
/** /**
* Prints a list * Prints the elements of a list to standard output.
* *
* @param toPrint - a list which should be printed * @param listToPrint the list to print
*/ */
static void print(List<?> toPrint) { static void print(List<?> listToPrint) {
toPrint String result = listToPrint.stream()
.stream() .map(Object::toString)
.map(Object::toString) .collect(Collectors.joining(" "));
.map(str -> str + " ") System.out.println(result);
.forEach(System.out::print);
System.out.println();
} }
/** /**
* Prints an array * Prints the elements of an array to standard output.
* *
* @param toPrint - an array which should be printed * @param array the array to print
*/ */
static void print(Object[] toPrint) { static <T> void print(T[] array) {
System.out.println(Arrays.toString(toPrint)); System.out.println(Arrays.toString(array));
} }
/** /**
* Swaps all position from { * Flips the order of elements in the specified range of an array.
* *
* @param left} to @{ * @param array the array whose elements are to be flipped
* @param right} for { * @param left the left boundary of the range to be flipped (inclusive)
* @param array} * @param right the right boundary of the range to be flipped (inclusive)
*
* @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) { public static <T extends Comparable<T>> void flip(T[] array, int left, int right) {
while (left <= right) { while (left <= right) {
swap(array, left++, right--); swap(array, left++, right--);
} }
} }
/** /**
* Function to check if the array is sorted. By default, it will check if the array is sorted in ASC order. * Checks whether the array is sorted in ascending order.
* *
* @param array - an array which to check is it sorted or not. * @param array the array to check
* @return true - if array sorted in ASC order, false otherwise. * @return true if the array is sorted in ascending order, false otherwise
*/ */
static <T extends Comparable<T>> boolean isSorted(T[] array) { public static <T extends Comparable<T>> boolean isSorted(T[] array) {
for (int i = 1; i < array.length; i++) for (int i = 1; i < array.length; i++) {
if (less(array[i], array[i - 1])) if (less(array[i], array[i - 1])) {
return false; return false;
}
}
return true; return true;
} }
static <T extends Comparable<T>> boolean isSorted(List<T> list) { /**
for (int i = 1; i < list.size(); i++) * Checks whether the list is sorted in ascending order.
if (less(list.get(i), list.get(i - 1))) *
* @param list the list to check
* @return true if the list is sorted in ascending order, false otherwise
*/
public static <T extends Comparable<T>> boolean isSorted(List<T> list) {
for (int i = 1; i < list.size(); i++) {
if (less(list.get(i), list.get(i - 1))) {
return false; return false;
}
}
return true; return true;
} }
} }