From 8b8c66e45faf6b0471847663015b5c466a30a2ae Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:02:09 +0800 Subject: [PATCH 1/2] [add] template code --- Heap.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Heap.java diff --git a/Heap.java b/Heap.java new file mode 100644 index 0000000..dd6535a --- /dev/null +++ b/Heap.java @@ -0,0 +1,30 @@ + +public class Heap>{ + public T[] heap; + public int SIZE; + public int count; + + Heap(int size){ + SIZE = size; + // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable + heap = (T[])new Comparable[SIZE]; + count = 0; + } + + + public void add (T item){ + if(count >= SIZE){ + // throw new Exception("Heap Overflow"); + System.out.println("Heap Full"); + return; + } + + } + + public void delete(T item){ + + } + + public void print(){ + } +} \ No newline at end of file -- Gitee From 4cf9e8fd9abb4fc56ae2da4af0c1744d3018431a Mon Sep 17 00:00:00 2001 From: XIE <18950888253@qq.com> Date: Tue, 15 Dec 2020 21:56:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Heap.java | 67 ++++++++++++++++++++++++++++++++++++++---------- HeapSort.java | 21 +++++++++++++++ HeapSortMin.java | 17 ++++++++++++ 3 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 HeapSort.java create mode 100644 HeapSortMin.java diff --git a/Heap.java b/Heap.java index dd6535a..b3feb38 100644 --- a/Heap.java +++ b/Heap.java @@ -1,30 +1,69 @@ +public class Heap> { + private T[] heap; + private int size; + private int count; -public class Heap>{ - public T[] heap; - public int SIZE; - public int count; - - Heap(int size){ - SIZE = size; + Heap(int size) { + this.size = size + 1; // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable - heap = (T[])new Comparable[SIZE]; + heap = (T[]) new Comparable[this.size]; count = 0; } - - public void add (T item){ - if(count >= SIZE){ + 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; } - public void delete(T item){ - + //isNull + public boolean isNull() { + return count == 0; } - public void print(){ + 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 0000000..7b4c39a --- /dev/null +++ b/HeapSort.java @@ -0,0 +1,21 @@ +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