From 8b8c66e45faf6b0471847663015b5c466a30a2ae Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:02:09 +0800 Subject: [PATCH 1/3] [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 55ec9afaa8d1094332f21b204b2cd5d0005fa941 Mon Sep 17 00:00:00 2001 From: hmy <1366553174@qq.com> Date: Tue, 15 Dec 2020 21:39:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Heap.java | 84 +++++++++++++++++++++++++++++++++++++-------------- HeapSort.java | 21 +++++++++++++ Main.java | 24 +++++++++++++++ 3 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 HeapSort.java create mode 100644 Main.java diff --git a/Heap.java b/Heap.java index dd6535a..75f0932 100644 --- a/Heap.java +++ b/Heap.java @@ -1,30 +1,70 @@ -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 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; + 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..55dff36 --- /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 Date: Wed, 16 Dec 2020 09:35:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=A0=86=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HeapSort.java | 38 ++++++++++++++++++++++++++++++++++++++ Main.java | 4 +++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/HeapSort.java b/HeapSort.java index 55dff36..32cc8df 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -18,4 +18,42 @@ public class HeapSort> { return tArray; } + /** + * 修改版堆排序 + */ + public T[] newSort(T[] array){ + int length = array.length; + for (int i=length-1;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 index f41bbc4..2cc3c74 100644 --- a/Main.java +++ b/Main.java @@ -16,9 +16,11 @@ public class Main { 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); - System.out.println("原始数组:"+ Arrays.toString(strArrey)); + Comparable[] newSortArrey = heapSort.newSort(strArrey); System.out.println("堆排序:"+Arrays.toString(sortArrey)); + System.out.println("新的堆排序:"+Arrays.toString(newSortArrey)); } } -- Gitee