diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..75f0932e80f1eb864392aa26851386c845cd3d3c --- /dev/null +++ b/Heap.java @@ -0,0 +1,70 @@ + +public class Heap> { + private T[] heap; + private int size; + private int count; + + Heap(int size) { + this.size = size + 1; + // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable + heap = (T[]) new Comparable[this.size]; + count = 0; + } + + public void add(T item) { + if (count >= size) { + // throw new Exception("Heap Overflow"); + System.out.println("Heap Full"); + return; + } + heap[++count] = item; + int position = count; + while ((position >> 1) >= 1 && heap[position].compareTo(heap[position >> 1]) > 0) { + //成立交换位置 + T t = heap[position]; + heap[position] = heap[position >> 1]; + heap[position >> 1] = t; + position = position >> 1; + } + } + + public T removeTop() { + if (isNull()) { + System.out.println("Heap Null"); + return null; + } + T returnData = heap[1]; + heap[1] = heap[count--];//将最后一个拿到第一位 + int position = 1; + while (true) { + int positionBackup = position; + if ((2 * position) <= count && heap[2 * position].compareTo(heap[positionBackup]) > 0) { + positionBackup = 2 * position; + } + if ((2 * position + 1) <= count && heap[2 * position + 1].compareTo(heap[positionBackup]) > 0) { + positionBackup = 2 * position + 1; + } + if (positionBackup == position) { + break;//无需交换退出循环 + } + T t = heap[position]; + heap[position] = heap[positionBackup]; + heap[positionBackup] = t; + position = positionBackup; + } + return returnData; + } + + //isNull + public boolean isNull() { + return count == 0; + } + + public void print() { + String str = ""; + for (int i = 1; i < count; i++) { + str += heap[i] + " "; + } + System.out.println(str); + } +} \ No newline at end of file diff --git a/HeapSort.java b/HeapSort.java new file mode 100644 index 0000000000000000000000000000000000000000..32cc8df8ac035881d0b2125b2f4a570cffe66092 --- /dev/null +++ b/HeapSort.java @@ -0,0 +1,59 @@ +public class HeapSort> { + + /** + * 堆排序 + * @param array + * @return + */ + public T[] sort(T[] array) { + int length = array.length; + Heap heap = new Heap(length); + for (int i=0;i=0;i--){ + //部分整合 + partialSort(array,i,length-1); + } + T[] tArray = (T[]) new Comparable[length]; + for (int i=0;i 0) { + positionBackup = 2 * position; + } + if ((2 * position + 1) <= count && heap[2 * position + 1].compareTo(heap[positionBackup]) > 0) { + positionBackup = 2 * position + 1; + } + if (positionBackup == position) { + break;//无需交换退出循环 + } + T t = heap[position]; + heap[position] = heap[positionBackup]; + heap[positionBackup] = t; + position = positionBackup; + } + } + + +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..2cc3c74aedeace695e55db74b0e06758f23c6e5a --- /dev/null +++ b/Main.java @@ -0,0 +1,26 @@ +import java.util.Arrays; +public class Main { + + private static final int HeapSize = 30; + + public static void main(String[] args) { + Heap heap = new Heap(HeapSize); + System.out.println("—————————————————————————————————————⼤顶堆—————————————————————————————"); + for (int i = 0; i < HeapSize; i++) { + heap.add((Integer)i); + } + heap.print(); + heap.removeTop(); + heap.print(); + heap.removeTop(); + heap.print(); + System.out.println("—————————————————————————————————————堆排序—————————————————————————————"); + String [] strArrey = {"5","6","9","1","8","2","7"}; + System.out.println("原始数组:"+ Arrays.toString(strArrey)); + HeapSort heapSort=new HeapSort(); + Comparable[] sortArrey = heapSort.sort(strArrey); + Comparable[] newSortArrey = heapSort.newSort(strArrey); + System.out.println("堆排序:"+Arrays.toString(sortArrey)); + System.out.println("新的堆排序:"+Arrays.toString(newSortArrey)); + } +}