2018-11-14 01:15:47 +08:00
|
|
|
package Sorts;
|
2018-04-10 02:14:40 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
import static Sorts.SortUtils.*;
|
|
|
|
|
2018-04-10 16:16:28 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-04-18 22:57:17 +08:00
|
|
|
/**
|
2020-10-24 18:23:28 +08:00
|
|
|
* Heap Sort Algorithm Implements MinHeap
|
2018-04-10 17:48:21 +08:00
|
|
|
*
|
|
|
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
2017-04-18 22:57:17 +08:00
|
|
|
*/
|
2018-04-10 16:16:28 +08:00
|
|
|
public class HeapSort implements SortAlgorithm {
|
2017-04-18 22:57:17 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private static class Heap<T extends Comparable<T>> {
|
|
|
|
/** Array to store heap */
|
|
|
|
private T[] heap;
|
2017-04-18 22:57:17 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param heap array of unordered integers
|
|
|
|
*/
|
|
|
|
public Heap(T[] heap) {
|
|
|
|
this.heap = heap;
|
|
|
|
}
|
2017-04-18 22:57:17 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Heapifies subtree from top as root to last as last child
|
|
|
|
*
|
|
|
|
* @param rootIndex index of root
|
|
|
|
* @param lastChild index of last child
|
|
|
|
*/
|
|
|
|
private void heapSubtree(int rootIndex, int lastChild) {
|
|
|
|
int leftIndex = rootIndex * 2 + 1;
|
|
|
|
int rightIndex = rootIndex * 2 + 2;
|
|
|
|
T root = heap[rootIndex];
|
|
|
|
if (rightIndex <= lastChild) { // if has right and left children
|
|
|
|
T left = heap[leftIndex];
|
|
|
|
T right = heap[rightIndex];
|
|
|
|
if (less(left, right) && less(left, root)) {
|
|
|
|
swap(heap, leftIndex, rootIndex);
|
|
|
|
heapSubtree(leftIndex, lastChild);
|
|
|
|
} else if (less(right, root)) {
|
|
|
|
swap(heap, rightIndex, rootIndex);
|
|
|
|
heapSubtree(rightIndex, lastChild);
|
2018-04-10 16:16:28 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
} else if (leftIndex <= lastChild) { // if no right child, but has left child
|
|
|
|
T left = heap[leftIndex];
|
|
|
|
if (less(left, root)) {
|
|
|
|
swap(heap, leftIndex, rootIndex);
|
|
|
|
heapSubtree(leftIndex, lastChild);
|
2017-04-18 22:57:17 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
}
|
2017-04-18 22:57:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Makes heap with root as root
|
|
|
|
*
|
|
|
|
* @param root index of root of heap
|
|
|
|
*/
|
|
|
|
private void makeMinHeap(int root) {
|
|
|
|
int leftIndex = root * 2 + 1;
|
|
|
|
int rightIndex = root * 2 + 2;
|
|
|
|
boolean hasLeftChild = leftIndex < heap.length;
|
|
|
|
boolean hasRightChild = rightIndex < heap.length;
|
|
|
|
if (hasRightChild) { // if has left and right
|
|
|
|
makeMinHeap(leftIndex);
|
|
|
|
makeMinHeap(rightIndex);
|
|
|
|
heapSubtree(root, heap.length - 1);
|
|
|
|
} else if (hasLeftChild) {
|
|
|
|
heapSubtree(root, heap.length - 1);
|
|
|
|
}
|
2017-04-18 22:57:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Gets the root of heap
|
|
|
|
*
|
|
|
|
* @return root of heap
|
|
|
|
*/
|
|
|
|
private T getRoot(int size) {
|
|
|
|
swap(heap, 0, size);
|
|
|
|
heapSubtree(0, size - 1);
|
|
|
|
return heap[size]; // return old root
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 16:16:28 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
@Override
|
|
|
|
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
|
|
|
|
return sort(Arrays.asList(unsorted)).toArray(unsorted);
|
|
|
|
}
|
2018-04-10 16:16:28 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
@Override
|
|
|
|
public <T extends Comparable<T>> List<T> sort(List<T> unsorted) {
|
|
|
|
int size = unsorted.size();
|
2018-04-10 16:16:28 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Heap<T> heap = new Heap<>(unsorted.toArray((T[]) new Comparable[unsorted.size()]));
|
2017-04-18 22:57:17 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
heap.makeMinHeap(0); // make min heap using index 0 as root.
|
|
|
|
List<T> sorted = new ArrayList<>(size);
|
|
|
|
while (size > 0) {
|
|
|
|
T min = heap.getRoot(--size);
|
|
|
|
sorted.add(min);
|
2017-04-18 22:57:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
return sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main method
|
|
|
|
*
|
|
|
|
* @param args the command line arguments
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Integer[] heap = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
|
|
|
HeapSort heapSort = new HeapSort();
|
|
|
|
print(heapSort.sort(heap));
|
|
|
|
}
|
2017-04-18 22:57:17 +08:00
|
|
|
}
|