From 8b8c66e45faf6b0471847663015b5c466a30a2ae Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:02:09 +0800 Subject: [PATCH 1/7] [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 c204d9d38d1a1887c5e56f027814d045f97d96dc Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:06:55 +0800 Subject: [PATCH 2/7] update README.md. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e931f2f..9dd8882 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ### 基础 -1. 实现⼀个小顶堆或者⼤顶堆 +1. 实现小顶堆或者大顶堆 - (1) 定义堆的数据结构 - (2) 实现入堆函数 - (3) 实现出堆函数 @@ -20,7 +20,7 @@ ### 进阶 1. 利用优先级队列合并K个有序数组 -2. 求⼀组动态数据集合的最⼤Top K +2. 求一组动态数据集合的Top K ## 工作流程 -- Gitee From c3305e480f1e72f57694a54a4d78215ca818c675 Mon Sep 17 00:00:00 2001 From: zan Date: Wed, 9 Dec 2020 08:07:50 +0800 Subject: [PATCH 3/7] update README.md. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9dd8882..41aca87 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ ### 截止日期 -下次实验课上课前(12.16) +- 基础-1 下次实验课上课前(12.16) +- 基础-2 12.23 ### 基础 -- Gitee From fe36aa9323227922e882d1be64a1db25adde639a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E9=9A=90?= <877146903@qq.com> Date: Tue, 15 Dec 2020 22:20:39 +0800 Subject: [PATCH 4/7] add ma.java. --- ma.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ma.java diff --git a/ma.java b/ma.java new file mode 100644 index 0000000..b401eb3 --- /dev/null +++ b/ma.java @@ -0,0 +1,24 @@ +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"}; + HeapSort heapSort=new HeapSort(); + Comparable[] sortArrey = heapSort.sort(strArrey); + System.out.println("原始数组:"+ Arrays.toString(strArrey)); + System.out.println("堆排序:"+Arrays.toString(sortArrey)); + } +} \ No newline at end of file -- Gitee From 3a81f8060466906ee13764a90dd0ab0f851e70e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E9=9A=90?= <877146903@qq.com> Date: Tue, 15 Dec 2020 22:21:59 +0800 Subject: [PATCH 5/7] add HeapSort.java. --- HeapSort.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 HeapSort.java diff --git a/HeapSort.java b/HeapSort.java new file mode 100644 index 0000000..01e407a --- /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 Date: Tue, 15 Dec 2020 22:23:00 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Heap.java | 85 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/Heap.java b/Heap.java index dd6535a..cfdfa57 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; - // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable - heap = (T[])new Comparable[SIZE]; - count = 0; - } - + 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 -- Gitee From 379cbd047b747c6846c9f4623349860f68f322b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E9=9A=90?= <877146903@qq.com> Date: Tue, 15 Dec 2020 22:23:19 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Heap.java | 1 + HeapSort.java | 1 + ma.java | 1 + 3 files changed, 3 insertions(+) diff --git a/Heap.java b/Heap.java index cfdfa57..93a2b32 100644 --- a/Heap.java +++ b/Heap.java @@ -53,6 +53,7 @@ public class Heap> { } return returnData; } + //isNull public boolean isNull() { diff --git a/HeapSort.java b/HeapSort.java index 01e407a..2395da6 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -1,5 +1,6 @@ public class HeapSort> { + public T[] sort(T[] array) { int length = array.length; Heap heap = new Heap(length); diff --git a/ma.java b/ma.java index b401eb3..b10cf57 100644 --- a/ma.java +++ b/ma.java @@ -1,6 +1,7 @@ import java.util.Arrays; public class Main { + private static final int HeapSize = 30; public static void main(String[] args) { -- Gitee