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

Code before:
        }

        final List<List<T>> piles = formPiles(array);
        final PriorityQueue<PileNode<T>> pq = mergePiles(piles);
        extractPiles(array, pq);

        return array;
    }

    /**
     * Forms piles from the given array. Each pile is a list of elements where
     * each element is smaller than the one before it. Binary search is used
     * to find the appropriate pile for each element.
     *
     * @param array the array of elements to be organized into piles
     * @param <T> the type of elements in the array, must be comparable
     * @return a list of piles
     */
    private static <T extends Comparable<T>> List<List<T>> formPiles(final T[] array) {
        final List<List<T>> piles = new ArrayList<>();
        final List<T> lastElements = new ArrayList<>();

        for (T x : array) {
            int pos = Collections.binarySearch(lastElements, x);
            if (pos < 0) {
                pos = ~pos; // bitwise complement, which is equivalent to (-pos - 1)
            }

            if (pos < piles.size()) {
                piles.get(pos).add(x);
                lastElements.set(pos, x);
            } else {
                List<T> newPile = new ArrayList<>();
                newPile.add(x);
                piles.add(newPile);
                lastElements.add(x);
            }
        }

        return piles;
    }

    /**
     * Merges the piles into a priority queue where the smallest elements are
     * prioritized.
     *
     * @param piles the list of piles to be merged
     * @param <T> the type of elements in the piles, must be comparable
     * @return a priority queue containing the top element of each pile
     */
    private static <T extends Comparable<T>> PriorityQueue<PileNode<T>> mergePiles(final List<List<T>> piles) {

Review comment (suggestion):
Why not just simply:
```suggestion
                pos = -pos - 1;
```
`pos = ~pos` _assumes_ how the integers are represented etc. With `pos = -pos - 1` I immediately see that if `pos < 0`, then `pos` becomes non-negative.

Code after:
        }

        final List<List<T>> piles = formPiles(array);
        final PriorityQueue<PileNode<T>> pq = mergePiles(piles);
        extractPiles(array, pq);

        return array;
    }

    /**
     * Forms piles from the given array. Each pile is a list of elements where
     * each element is smaller than the one before it. Binary search is used
     * to find the appropriate pile for each element.
     *
     * @param array the array of elements to be organized into piles
     * @param <T> the type of elements in the array, must be comparable
     * @return a list of piles
     */
    private static <T extends Comparable<T>> List<List<T>> formPiles(final T[] array) {
        final List<List<T>> piles = new ArrayList<>();
        final List<T> lastElements = new ArrayList<>();

        for (T x : array) {
            int pos = Collections.binarySearch(lastElements, x);
            if (pos < 0) {
                pos = -pos - 1;
            }

            if (pos < piles.size()) {
                piles.get(pos).add(x);
                lastElements.set(pos, x);
            } else {
                List<T> newPile = new ArrayList<>();
                newPile.add(x);
                piles.add(newPile);
                lastElements.add(x);
            }
        }

        return piles;
    }

    /**
     * Merges the piles into a priority queue where the smallest elements are
     * prioritized.
     *
     * @param piles the list of piles to be merged
     * @param <T> the type of elements in the piles, must be comparable
     * @return a priority queue containing the top element of each pile
     */
    private static <T extends Comparable<T>> PriorityQueue<PileNode<T>> mergePiles(final List<List<T>> piles) {