Skip to content

update SortUtils #4139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/thealgorithms/sorts/CombSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
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;
}
}
}
Expand Down
129 changes: 63 additions & 66 deletions src/main/java/com/thealgorithms/sorts/SortUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,118 @@

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;
}
}