Skip to content

#6336/enhancement/Refactor: Standardize comparison logic using SortUtils #6442

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mi
array[k] = aux[j++];
} else if (j > high) {
array[k] = aux[i++];
} else if (aux[j].compareTo(aux[i]) < 0) {
} else if (SortUtils.less(aux[j], aux[i])) {
array[k] = aux[j++];
} else {
array[k] = aux[i++];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {

while (low <= high) {
final int mid = (low + high) >>> 1;
if (temp.compareTo(array[mid]) < 0) {
if (SortUtils.less(temp, array[mid])) {
high = mid - 1;
} else {
low = mid + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/sorts/BitonicSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private <T extends Comparable<T>> void bitonicMerge(T[] array, int low, int cnt,
if (cnt > 1) {
final int k = cnt / 2;

final BiPredicate<T, T> areSorted = (direction == Direction.ASCENDING) ? (a, b) -> a.compareTo(b) < 0 : (a, b) -> a.compareTo(b) > 0;
final BiPredicate<T, T> areSorted = (direction == Direction.ASCENDING) ? (a, b) -> SortUtils.less(a, b) : (a, b) -> SortUtils.greater(a, b);
for (int i = low; i < low + k; i++) {
if (!areSorted.test(array[i], array[i + k])) {
SortUtils.swap(array, i, i + k);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/sorts/BucketSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private <T extends Comparable<T>> int hash(final T element, final T min, final T
private <T extends Comparable<T>> T findMin(T[] array) {
T min = array[0];
for (T element : array) {
if (element.compareTo(min) < 0) {
if (SortUtils.less(element, min)) {
min = element;
}
}
Expand All @@ -121,7 +121,7 @@ private <T extends Comparable<T>> T findMin(T[] array) {
private <T extends Comparable<T>> T findMax(T[] array) {
T max = array[0];
for (T element : array) {
if (element.compareTo(max) > 0) {
if (SortUtils.greater(element, max)) {
max = element;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/sorts/CircleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ private <T extends Comparable<T>> boolean doSort(final T[] array, final int left
int high = right;

while (low < high) {
if (array[low].compareTo(array[high]) > 0) {
if (SortUtils.greater(array[low], array[high])) {
SortUtils.swap(array, low, high);
swapped = true;
}
low++;
high--;
}

if (low == high && array[low].compareTo(array[high + 1]) > 0) {
if (low == high && SortUtils.greater(array[low], array[high + 1])) {
SortUtils.swap(array, low, high + 1);
swapped = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ private <T extends Comparable<T>> T[] dutchNationalFlagSort(final T[] array, fin
int k = array.length - 1;

while (j <= k) {
if (0 > array[j].compareTo(intendedMiddle)) {
if (SortUtils.less(array[j], intendedMiddle)) {
SortUtils.swap(array, i, j);
j++;
i++;
} else if (0 < array[j].compareTo(intendedMiddle)) {
} else if (SortUtils.greater(array[j], intendedMiddle)) {
SortUtils.swap(array, j, k);
k--;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/sorts/ExchangeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ExchangeSort implements SortAlgorithm {
public <T extends Comparable<T>> T[] sort(T[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i].compareTo(array[j]) > 0) {
if (SortUtils.greater(array[i], array[j])) {
SortUtils.swap(array, i, j);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static <T extends Comparable<T>> int partition(T[] array, final int low,
final T pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j].compareTo(pivot) <= 0) {
if (SortUtils.greaterOrEqual(pivot, array[j])) {
i++;
SortUtils.swap(array, i, j);
}
Expand All @@ -84,7 +84,7 @@ private static <T extends Comparable<T>> void insertionSort(T[] array, final int
for (int i = low + 1; i <= high; i++) {
final T key = array[i];
int j = i - 1;
while (j >= low && array[j].compareTo(key) > 0) {
while (j >= low && SortUtils.greater(array[j], key)) {
array[j + 1] = array[j];
j--;
}
Expand Down Expand Up @@ -125,10 +125,10 @@ private static <T extends Comparable<T>> void heapify(T[] array, final int i, fi
final int right = 2 * i + 2;
int largest = i;

if (left < n && array[low + left].compareTo(array[low + largest]) > 0) {
if (left < n && SortUtils.greater(array[low + left], array[low + largest])) {
largest = left;
}
if (right < n && array[low + right].compareTo(array[low + largest]) > 0) {
if (right < n && SortUtils.greater(array[low + right], array[low + largest])) {
largest = right;
}
if (largest != i) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/sorts/OddEvenSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
private <T extends Comparable<T>> boolean performOddSort(T[] array) {
boolean sorted = true;
for (int i = 1; i < array.length - 1; i += 2) {
if (array[i].compareTo(array[i + 1]) > 0) {
if (SortUtils.greater(array[i], array[i + 1])) {
SortUtils.swap(array, i, i + 1);
sorted = false;
}
Expand All @@ -41,7 +41,7 @@ private <T extends Comparable<T>> boolean performOddSort(T[] array) {
private <T extends Comparable<T>> boolean performEvenSort(T[] array) {
boolean sorted = true;
for (int i = 0; i < array.length - 1; i += 2) {
if (array[i].compareTo(array[i + 1]) > 0) {
if (SortUtils.greater(array[i], array[i + 1])) {
SortUtils.swap(array, i, i + 1);
sorted = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/sorts/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
private static <T extends Comparable<T>> int findIndexOfMin(T[] array, final int startIndex) {
int minIndex = startIndex;
for (int i = startIndex + 1; i < array.length; i++) {
if (array[i].compareTo(array[minIndex]) < 0) {
if (SortUtils.less(array[i], array[minIndex])) {
minIndex = i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ private static <T extends Comparable<T>> int findMinIndex(T[] array, final int s
final int minIndexInRest = findMinIndex(array, start + 1);

// Return the index of the smaller element between array[start] and the minimum element in the rest of the array
return array[start].compareTo(array[minIndexInRest]) < 0 ? start : minIndexInRest;
return SortUtils.less(array[start], array[minIndexInRest]) ? start : minIndexInRest;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/sorts/StalinSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
}
int currentIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[i].compareTo(array[currentIndex]) >= 0) {
if (SortUtils.greaterOrEqual(array[i], array[currentIndex])) {
currentIndex++;
array[currentIndex] = array[i];
}
Expand Down