feat: add PatienceSort (#5288)

* feat: PatienceSort

* refactor: fix readability issues,a and redundant check

---------

Co-authored-by: alxklm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-07-24 18:32:47 +03:00 committed by GitHub
parent 08db744240
commit 76a450fb75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 122 additions and 0 deletions

View File

@ -511,6 +511,7 @@
* [MergeSortRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/MergeSortRecursive.java) * [MergeSortRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/MergeSortRecursive.java)
* [OddEvenSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/OddEvenSort.java) * [OddEvenSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/OddEvenSort.java)
* [PancakeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/PancakeSort.java) * [PancakeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/PancakeSort.java)
* [PatienceSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/PatienceSort.java)
* [PigeonholeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java) * [PigeonholeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java)
* [QuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/QuickSort.java) * [QuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/QuickSort.java)
* [RadixSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/RadixSort.java) * [RadixSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/RadixSort.java)
@ -881,6 +882,7 @@
* [MergeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/MergeSortTest.java) * [MergeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/MergeSortTest.java)
* [OddEvenSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java) * [OddEvenSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java)
* [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java) * [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java)
* [PatienceSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PatienceSortTest.java)
* [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java) * [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java)
* [RadixSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/RadixSortTest.java) * [RadixSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/RadixSortTest.java)
* [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java) * [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java)

View File

@ -0,0 +1,112 @@
package com.thealgorithms.sorts;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
/**
* This class implements the Patience Sort algorithm. Patience Sort is a sorting algorithm that
* is particularly good for sorting sequences that are already partially sorted.
*/
public class PatienceSort implements SortAlgorithm {
/**
* Sorts an array of comparable elements using the Patience Sort algorithm.
*
* @param array the array to be sorted
* @param <T> the type of elements in the array, must be comparable
* @return the sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
if (array.length == 0) {
return array;
}
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) {
PriorityQueue<PileNode<T>> pq = new PriorityQueue<>();
for (List<T> pile : piles) {
pq.add(new PileNode<>(pile.removeLast(), pile));
}
return pq;
}
/**
* Extracts elements from the priority queue to form the sorted array.
*
* @param array the array to be filled with sorted elements
* @param pq the priority queue containing the elements to be extracted
* @param <T> the type of elements in the array, must be comparable
*/
private static <T extends Comparable<T>> void extractPiles(final T[] array, final PriorityQueue<PileNode<T>> pq) {
int index = 0;
while (!pq.isEmpty()) {
PileNode<T> node = pq.poll();
array[index++] = node.value;
if (!node.pile.isEmpty()) {
pq.add(new PileNode<>(node.pile.removeLast(), node.pile));
}
}
}
/**
* A helper record representing a node in the priority queue.
*
* @param <T> the type of elements in the node, must be comparable
*/
private record PileNode<T extends Comparable<T>>(T value, List<T> pile) implements Comparable<PileNode<T>> {
@Override
public int compareTo(PileNode<T> other) {
return this.value.compareTo(other.value);
}
}
}

View File

@ -0,0 +1,8 @@
package com.thealgorithms.sorts;
public class PatienceSortTest extends SortingAlgorithmTest {
@Override
SortAlgorithm getSortAlgorithm() {
return new PatienceSort();
}
}