Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/BitonicSort.java (Java)

Code before:
            bitonicSort(arr, low, k, Direction.ASCENDING);

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

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

    /**
     * Merges the bitonic sequence in the specified direction.
     *
     * @param <T> the type of elements in the array, which must be Comparable
     * @param arr 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 dir the direction of sorting: true for ascending, false for descending
     */
    private <T extends Comparable<T>> void bitonicMerge(T[] arr, int low, int cnt, Direction dir) {
        if (cnt > 1) {
            final int k = cnt / 2;

            for (int i = low; i < low + k; i++) {
                boolean condition = (dir == Direction.ASCENDING) ? arr[i].compareTo(arr[i + k]) > 0 : arr[i].compareTo(arr[i + k]) < 0;
                if (condition) {
                    SortUtils.swap(arr, i, i + k);
                }
            }

            bitonicMerge(arr, low, k, dir);
            bitonicMerge(arr, low + k, cnt - k, dir);
        }
    }

    /**
     * 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;

Review comment (suggestion):
Maybe this will be more readable:
```suggestion
            final BiPredicate<T, T> areSorted = (dir == 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(arr[i], arr[i + k])) {
```

Code after:
            // 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) {