From e607a36f2a1f0df7d801f72a9ff975e1707ddeed Mon Sep 17 00:00:00 2001 From: yihanz1 <719351071@qq.com> Date: Tue, 15 Dec 2020 19:37:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A4=A7=E9=A1=B6=E5=A0=86=E5=9F=BA?= =?UTF-8?q?=E7=A1=801?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HomeWork/Heap.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++ HomeWork/Test.java | 21 +++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 HomeWork/Heap.java create mode 100644 HomeWork/Test.java diff --git a/HomeWork/Heap.java b/HomeWork/Heap.java new file mode 100644 index 0000000..b1b0bda --- /dev/null +++ b/HomeWork/Heap.java @@ -0,0 +1,59 @@ +public class Heap> { + public T[] heap; + public int SIZE; + public int count; + + Heap(int size) { + SIZE = size; + heap = (T[]) new Comparable[SIZE]; + count = 0; + } + + public void add(T item) { + if (count >= SIZE) { + System.out.println("Heap Full"); + return; + } + heap[count] = item; + count++; + int tempValue = count - 1; + while (true) { + if (heap[tempValue].compareTo(heap[(tempValue - 1) / 2]) > 0) { + T tempNode = heap[(tempValue - 1) / 2]; + heap[(tempValue - 1) / 2] = heap[tempValue]; + heap[tempValue] = tempNode; + tempValue = (tempValue - 1) / 2; + } else { + break; + } + } + } + + public void delete() { + heap[0] = heap[count - 1]; + heap[count - 1] = null; + count--; + for (int i = 0; i < count / 2; i++) { + if (heap[i * 2 + 1] != null) { + if (heap[i].compareTo(heap[i * 2 + 1]) < 0) { + T temp = heap[i]; + heap[i] = heap[i * 2 + 1]; + heap[i * 2 + 1] = temp; + } + } + if (heap[i * 2 + 2] != null) { + if (heap[i].compareTo(heap[i * 2 + 2]) < 0) { + T temp = heap[i]; + heap[i] = heap[i * 2 + 2]; + heap[i * 2 + 2] = temp; + } + } + } + } + + public void print() { + for (int i = 0; i < count; i++) { + System.out.print(" " + heap[i]); + } + } +} \ No newline at end of file diff --git a/HomeWork/Test.java b/HomeWork/Test.java new file mode 100644 index 0000000..2bf2f47 --- /dev/null +++ b/HomeWork/Test.java @@ -0,0 +1,21 @@ +public class Test { + public static void main(String[] args) { + Heap heap = new Heap(10); + int nodeNum[] = new int[]{8, 7, 6, 20, 5, 4, 22}; + initHeap(heap, nodeNum); + heap.delete(); + heap.delete(); + System.out.print("\n删除了2次最大值后:"); + heap.print(); + + } + + private static void initHeap(Heap heap, int[] nodeNum) { + for (int i = 0; i < nodeNum.length; i++) { + heap.add(nodeNum[i]); + } + System.out.print("第一次构建堆:"); + heap.print(); + } + +} -- Gitee From b53a62835095b88a2a7a5bbef02fbbef25159e94 Mon Sep 17 00:00:00 2001 From: yihanz1 <719351071@qq.com> Date: Tue, 22 Dec 2020 18:13:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=A7=E9=A1=B6=E5=A0=86=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E5=85=A8=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HomeWork/Heap.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++ HomeWork/Test.java | 3 ++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/HomeWork/Heap.java b/HomeWork/Heap.java index b1b0bda..e111796 100644 --- a/HomeWork/Heap.java +++ b/HomeWork/Heap.java @@ -1,3 +1,4 @@ + public class Heap> { public T[] heap; public int SIZE; @@ -51,6 +52,57 @@ public class Heap> { } } + public void sortHeap(int[] arr) { + int arrCount = arr.length; + //先将数组先排序成大顶堆 + arr = sort(arrCount, arr); + //循环将堆顶和堆最后一个元素互换,然后除去最后一个元素,将数组继续排序成大顶堆 + while (arrCount/2>0){ + int temp=arr[0]; + arr[0]=arr[arrCount-1]; + arr[arrCount-1]=temp; + arrCount--; + arr= sort(arrCount,arr); + } + System.out.println(); + System.out.print("堆排序开始"); + for(int a:arr){ + System.out.print(" "+a); + } + } + + private int[] sort(int arrCount, int[] arr) { + for (int i = arrCount / 2 - 1; i >= 0; i--) { + //定义一个变量存放当前的位置 + int current = i; + //如果当前位置的左节点不为空,往下循环判断是不是大顶堆 + while ((current * 2 + 1) <= arrCount - 1) { + //定义一个变量存放左子树或者右子树中的最大值 + int maxNum = current; + //如果右子树不为空的话,两个系欸但值比较,否则左节点为最大值 + if ((current * 2 + 2) < arrCount) { + if (arr[current * 2 + 1] > arr[current * 2 + 2]) { + maxNum = current * 2 + 1; + } else { + maxNum = current * 2 + 2; + } + } else { + maxNum = current * 2 + 1; + } + //如果当前位置的节点小于左右中的最大节点,则把最大节点和当前位置互换,然后最大节点位置赋值给当前位置,当前位置继续进循环看是否满足顶堆 + if (arr[current] < arr[maxNum]) { + int temp = arr[current]; + arr[current] = arr[maxNum]; + arr[maxNum] = temp; + current = maxNum; + } else { + break; + } + } + } + return arr; + } + public void print() { for (int i = 0; i < count; i++) { System.out.print(" " + heap[i]); diff --git a/HomeWork/Test.java b/HomeWork/Test.java index 2bf2f47..e450f17 100644 --- a/HomeWork/Test.java +++ b/HomeWork/Test.java @@ -7,7 +7,8 @@ public class Test { heap.delete(); System.out.print("\n删除了2次最大值后:"); heap.print(); - + int[] arr = new int[]{5, 3, 9, 6, 15, 4, 11, 16, 20, 22, 25, 88, 66, 99, 77, 100, 8}; + heap.sortHeap(arr); } private static void initHeap(Heap heap, int[] nodeNum) { -- Gitee