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++) {
if (less(arr[i + gap], arr[i])) {
// 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.List;
import java.util.stream.Collectors;
/**
* The class contains util methods
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*/
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 idx index of the first element
* @param idy index of the second element
* @param array the array in which to swap elements
* @param i the index of the first element to swap
* @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) {
T swap = array[idx];
array[idx] = array[idy];
array[idy] = swap;
return true;
public static <T> void swap(T[] array, int i, int j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
/**
* 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 w second element
* @return true if the first element is less than the second element
* @param firstElement the first element to compare
* @param secondElement the second element to compare
* @return true if the first element is less than the second, false otherwise
*/
static <T extends Comparable<T>> boolean less(T v, T w) {
return v.compareTo(w) < 0;
public static <T extends Comparable<T>> boolean less(T firstElement, T secondElement) {
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 w second element
* @return true if the first element is greater than the second element
* @param firstElement the first element to compare
* @param secondElement the second element to compare
* @return true if the first element is greater than the second, false otherwise
*/
static <T extends Comparable<T>> boolean greater(T v, T w) {
return v.compareTo(w) > 0;
public static <T extends Comparable<T>> boolean greater(T firstElement, T secondElement) {
return firstElement.compareTo(secondElement) > 0;
}
/**
* This method checks if first element is greater than or equal the other
* element
* Compares two elements to see if the first is greater than or equal to the second.
*
* @param v first element
* @param w second element
* @return true if the first element is greater than or equal the second
* element
* @param firstElement the first element to compare
* @param secondElement the second element to compare
* @return true if the first element is greater than or equal to the second, false otherwise
*/
static <T extends Comparable<T>> boolean greaterOrEqual(T v, T w) {
return v.compareTo(w) >= 0;
static <T extends Comparable<T>> boolean greaterOrEqual(T firstElement, T secondElement) {
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) {
toPrint
.stream()
.map(Object::toString)
.map(str -> str + " ")
.forEach(System.out::print);
System.out.println();
static void print(List<?> listToPrint) {
String result = listToPrint.stream()
.map(Object::toString)
.collect(Collectors.joining(" "));
System.out.println(result);
}
/**
* 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) {
System.out.println(Arrays.toString(toPrint));
static <T> void print(T[] array) {
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 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
* @param array the array whose elements are to be flipped
* @param left the left boundary of the range to be flipped (inclusive)
* @param right the right boundary of the range to be flipped (inclusive)
*/
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) {
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.
* @return true - if array sorted in ASC order, false otherwise.
* @param array the array to check
* @return true if the array is sorted in ascending order, false otherwise
*/
static <T extends Comparable<T>> boolean isSorted(T[] array) {
for (int i = 1; i < array.length; i++)
if (less(array[i], array[i - 1]))
public static <T extends Comparable<T>> boolean isSorted(T[] array) {
for (int i = 1; i < array.length; i++) {
if (less(array[i], array[i - 1])) {
return false;
}
}
return true;
}
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)))
/**
* Checks whether the list is sorted in ascending order.
*
* @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 true;
}
}