Skip to content

Refactored all the compareTo() to SortUtils functions #6336 #6433

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

Closed
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
68 changes: 34 additions & 34 deletions src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package com.thealgorithms.sorts;

public class AdaptiveMergeSort implements SortAlgorithm {
@SuppressWarnings("unchecked")
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length <= 1) {
return array;
}
T[] aux = array.clone();
sort(array, aux, 0, array.length - 1);
return array;
}
@SuppressWarnings("unchecked")
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length <= 1) {
return array;
}
T[] aux = array.clone();
sort(array, aux, 0, array.length - 1);
return array;
}

private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
if (low >= high) {
return;
}
int mid = low + (high - low) / 2;
sort(array, aux, low, mid);
sort(array, aux, mid + 1, high);
merge(array, aux, low, mid, high);
}
private <T extends Comparable<T>> void sort(T[] array, T[] aux, int low, int high) {
if (low >= high) {
return;
}
int mid = low + (high - low) / 2;
sort(array, aux, low, mid);
sort(array, aux, mid + 1, high);
merge(array, aux, low, mid, high);
}

private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
System.arraycopy(array, low, aux, low, high - low + 1);
int i = low;
int j = mid + 1;
for (int k = low; k <= high; k++) {
if (i > mid) {
array[k] = aux[j++];
} else if (j > high) {
array[k] = aux[i++];
} else if (aux[j].compareTo(aux[i]) < 0) {
array[k] = aux[j++];
} else {
array[k] = aux[i++];
}
}
}
private <T extends Comparable<T>> void merge(T[] array, T[] aux, int low, int mid, int high) {
System.arraycopy(array, low, aux, low, high - low + 1);
int i = low;
int j = mid + 1;
for (int k = low; k <= high; k++) {
if (i > mid) {
array[k] = aux[j++];
} else if (j > high) {
array[k] = aux[i++];
} else if (SortUtils.less(aux[j], aux[i])) {
array[k] = aux[j++];
} else {
array[k] = aux[i++];
}
}
}
}
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
210 changes: 107 additions & 103 deletions src/main/java/com/thealgorithms/sorts/BitonicSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,109 +4,113 @@
import java.util.function.BiPredicate;

/**
* BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
* BitonicSort class implements the SortAlgorithm interface using the bitonic
* sort technique.
*/
public class BitonicSort implements SortAlgorithm {
private enum Direction {
DESCENDING,
ASCENDING,
}

/**
* Sorts the given array using the Bitonic Sort algorithm.
*
* @param <T> the type of elements in the array, which must implement the Comparable interface
* @param array the array to be sorted
* @return the sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length == 0) {
return array;
}

final int paddedSize = nextPowerOfTwo(array.length);
T[] paddedArray = Arrays.copyOf(array, paddedSize);

// Fill the padded part with a maximum value
final T maxValue = max(array);
Arrays.fill(paddedArray, array.length, paddedSize, maxValue);

bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
return Arrays.copyOf(paddedArray, array.length);
}

private <T extends Comparable<T>> void bitonicSort(final T[] array, final int low, final int cnt, final Direction direction) {
if (cnt > 1) {
final int k = cnt / 2;

// Sort first half in ascending order
bitonicSort(array, low, k, Direction.ASCENDING);

// Sort second half in descending order
bitonicSort(array, low + k, cnt - k, Direction.DESCENDING);

// Merge the whole sequence in ascending order
bitonicMerge(array, low, cnt, direction);
}
}

/**
* Merges the bitonic sequence in the specified direction.
*
* @param <T> the type of elements in the array, which must be Comparable
* @param array the array containing the bitonic sequence to be merged
* @param low the starting index of the sequence to be merged
* @param cnt the number of elements in the sequence to be merged
* @param direction the direction of sorting
*/
private <T extends Comparable<T>> void bitonicMerge(T[] array, int low, int cnt, Direction direction) {
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;
for (int i = low; i < low + k; i++) {
if (!areSorted.test(array[i], array[i + k])) {
SortUtils.swap(array, i, i + k);
}
}

bitonicMerge(array, low, k, direction);
bitonicMerge(array, low + k, cnt - k, direction);
}
}

/**
* Finds the next power of two greater than or equal to the given number.
*
* @param n the number
* @return the next power of two
*/
private static int nextPowerOfTwo(int n) {
int count = 0;

// First n in the below condition is for the case where n is 0
if ((n & (n - 1)) == 0) {
return n;
}

while (n != 0) {
n >>= 1;
count += 1;
}

return 1 << count;
}

/**
* Finds the maximum element in the given array.
*
* @param <T> the type of elements in the array, which must implement the Comparable interface
* @param array the array to be searched
* @return the maximum element in the array
* @throws IllegalArgumentException if the array is null or empty
*/
private static <T extends Comparable<T>> T max(final T[] array) {
return Arrays.stream(array).max(Comparable::compareTo).orElseThrow();
}
private enum Direction {
DESCENDING, ASCENDING,
}

/**
* Sorts the given array using the Bitonic Sort algorithm.
*
* @param <T> the type of elements in the array, which must implement the
* Comparable interface
* @param array the array to be sorted
* @return the sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length == 0) {
return array;
}

final int paddedSize = nextPowerOfTwo(array.length);
T[] paddedArray = Arrays.copyOf(array, paddedSize);

// Fill the padded part with a maximum value
final T maxValue = max(array);
Arrays.fill(paddedArray, array.length, paddedSize, maxValue);

bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
return Arrays.copyOf(paddedArray, array.length);
}

private <T extends Comparable<T>> void bitonicSort(final T[] array, final int low, final int cnt,
final Direction direction) {
if (cnt > 1) {
final int k = cnt / 2;

// Sort first half in ascending order
bitonicSort(array, low, k, Direction.ASCENDING);

// Sort second half in descending order
bitonicSort(array, low + k, cnt - k, Direction.DESCENDING);

// Merge the whole sequence in ascending order
bitonicMerge(array, low, cnt, direction);
}
}

/**
* Merges the bitonic sequence in the specified direction.
*
* @param <T> the type of elements in the array, which must be Comparable
* @param array the array containing the bitonic sequence to be merged
* @param low the starting index of the sequence to be merged
* @param cnt the number of elements in the sequence to be merged
* @param direction the direction of sorting
*/
private <T extends Comparable<T>> void bitonicMerge(T[] array, int low, int cnt, Direction direction) {
if (cnt > 1) {
final int k = cnt / 2;

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

bitonicMerge(array, low, k, direction);
bitonicMerge(array, low + k, cnt - k, direction);
}
}

/**
* Finds the next power of two greater than or equal to the given number.
*
* @param n the number
* @return the next power of two
*/
private static int nextPowerOfTwo(int n) {
int count = 0;

// First n in the below condition is for the case where n is 0
if ((n & (n - 1)) == 0) {
return n;
}

while (n != 0) {
n >>= 1;
count += 1;
}

return 1 << count;
}

/**
* Finds the maximum element in the given array.
*
* @param <T> the type of elements in the array, which must implement the
* Comparable interface
* @param array the array to be searched
* @return the maximum element in the array
* @throws IllegalArgumentException if the array is null or empty
*/
private static <T extends Comparable<T>> T max(final T[] array) {
return Arrays.stream(array).max(Comparable::compareTo).orElseThrow();
}
}
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy