diff --git a/src/main/java/com/thealgorithms/sorts/CombSort.java b/src/main/java/com/thealgorithms/sorts/CombSort.java index 7ddce43d..78951fb3 100644 --- a/src/main/java/com/thealgorithms/sorts/CombSort.java +++ b/src/main/java/com/thealgorithms/sorts/CombSort.java @@ -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; } } } diff --git a/src/main/java/com/thealgorithms/sorts/SortUtils.java b/src/main/java/com/thealgorithms/sorts/SortUtils.java index f9f99055..5f3563fb 100644 --- a/src/main/java/com/thealgorithms/sorts/SortUtils.java +++ b/src/main/java/com/thealgorithms/sorts/SortUtils.java @@ -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 the type of elements in the array */ - static boolean swap(T[] array, int idx, int idy) { - T swap = array[idx]; - array[idx] = array[idy]; - array[idy] = swap; - return true; + public static 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 > boolean less(T v, T w) { - return v.compareTo(w) < 0; + public static > 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 > boolean greater(T v, T w) { - return v.compareTo(w) > 0; + public static > 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 > boolean greaterOrEqual(T v, T w) { - return v.compareTo(w) >= 0; + static > 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 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 > void flip(T[] array, int left, int right) { + public static > 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 > boolean isSorted(T[] array) { - for (int i = 1; i < array.length; i++) - if (less(array[i], array[i - 1])) + public static > boolean isSorted(T[] array) { + for (int i = 1; i < array.length; i++) { + if (less(array[i], array[i - 1])) { return false; + } + } return true; } - static > boolean isSorted(List 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 > boolean isSorted(List list) { + for (int i = 1; i < list.size(); i++) { + if (less(list.get(i), list.get(i - 1))) { return false; + } + } return true; } }