diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ec068fd7224c68060f4d425518cb3a8b1638c780 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../:\zuoye\数据结构与算法分析\lec07-heap\.idea/dataSources/ +/dataSources.local.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..1bb046f99d2e15d791ad96d4cecedb01f0d2b55d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..b16effcb15582734970db92fdd35ef4b55314bc0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..10691ff8b3f342d8600a9d31de667bb930286f09 --- /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..030cc082fc1341bddb23e97a309b4ca41acfebe9 --- /dev/null +++ b/HeapSort.java @@ -0,0 +1,16 @@ + +public class HeapSort> { + public T[] sort(T[] array) { + int length = array.length; + Heap heap = new Heap(length); + for (int i=0;i + + + + + + + + + + \ No newline at end of file