diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..73f69e0958611ac6e00bde95641f6699030ad235 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000000000000000000000000000000000..6560a98983ec708cf9d8b5c5c3776d7bd39c475b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..e208459b8afde5f7980720efd6bbb97f7ae24541 --- /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/README.md b/README.md index e931f2f0393b17470887781a3e4325a8e502941d..41aca87a2caf0d420ec69ba324b7a612a5db5f15 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,12 @@ ### 截止日期 -下次实验课上课前(12.16) +- 基础-1 下次实验课上课前(12.16) +- 基础-2 12.23 ### 基础 -1. 实现⼀个小顶堆或者⼤顶堆 +1. 实现小顶堆或者大顶堆 - (1) 定义堆的数据结构 - (2) 实现入堆函数 - (3) 实现出堆函数 @@ -20,7 +21,7 @@ ### 进阶 1. 利用优先级队列合并K个有序数组 -2. 求⼀组动态数据集合的最⼤Top K +2. 求一组动态数据集合的Top K ## 工作流程 diff --git a/lec07-heap.iml b/lec07-heap.iml new file mode 100644 index 0000000000000000000000000000000000000000..6e2c7755aa917965e4842eeb80ad46d9d499fa86 --- /dev/null +++ b/lec07-heap.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Heap.java b/src/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..c4c63545fd6e22c327daaad910459165eccbeb57 --- /dev/null +++ b/src/Heap.java @@ -0,0 +1,74 @@ +import java.util.Arrays; + +public class Heap { + + private Integer[] head; + + public Heap(int length) { + this.head = new Integer[length]; + } + + public void insert(int data) { + System.out.println("插入:"+ data); + for (int i = 0;i < this.head.length;i++) { + if (this.head[i] == null) { + if (i == 0) { + this.head[0] = data; + return; + } + this.head[i] = data; + for (int j = i;j > 0; j = (j-1)/2) { + if (data < this.head[(j-1)/2]) { + this.head[j] = this.head[(j-1)/2]; + this.head[(j-1)/2] = data; + } + } + break; + } + } + } + + public Integer deleteMin() { + if (this.head[0] == null) { + System.out.println("堆不存在元素"); + return null; + } + int tempNone = 0; + int tempLast = 0; + int result = this.head[0]; + for (int i = 1;i < this.head.length;i++) { + if (this.head[i] == null || this.head[this.head.length-1] != null) { + i--; + if (this.head[this.head.length-1] != null) { + i = this.head.length-1; + } + tempLast = i; + this.head[0] = null; + for (int j = 1; j < i; j = j * 2 + 1){ + if (this.head[j+1] == null) { + this.head[(j-1)/2] = this.head[j]; + this.head[j] = null; + return result; + } + if (this.head[j] > this.head[j+1]) { + this.head[(j-1)/2] = this.head[j+1]; + this.head[j+1] = null; + j = j + 1; + }else { + this.head[(j-1)/2] = this.head[j]; + this.head[j] = null; + } + tempNone = j; + } + this.head[tempNone] = this.head[tempLast]; + this.head[tempLast] = null; + return result; + } + } + return result; + } + + public void print() { + System.out.println(Arrays.toString(this.head)); + } +} diff --git a/src/MainTest.java b/src/MainTest.java new file mode 100644 index 0000000000000000000000000000000000000000..310ddaabbfe475bda8f940dbb49e6c1494932ff8 --- /dev/null +++ b/src/MainTest.java @@ -0,0 +1,57 @@ +import java.util.Arrays; + +public class MainTest { + public static void main(String[] args) { +// Heap heap = new Heap(10); +// heap.insert(9); +// heap.insert(8); +// heap.insert(7); +// heap.insert(6); +// heap.insert(2); +// heap.insert(4); +// heap.insert(5); +// heap.insert(0); +// heap.insert(1); +// heap.insert(3); +// heap.print(); +// System.out.println("第一次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第二次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第三次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第四次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第五次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第六次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第七次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第八次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第九次删除"); +// heap.deleteMin(); +// heap.print(); +// System.out.println("第十次删除"); +// heap.deleteMin(); +// heap.print(); + int[] tar = {4,2,6,3,8,7,5}; + Heap sort = new Heap(tar.length); + for (int i : tar) { + sort.insert(i); + } + for (int i = 0; i < tar.length;i++) { + tar[i] = sort.deleteMin(); + } + System.out.println(Arrays.toString(tar)); + } +} diff --git "a/\350\257\276\344\273\266/lecture07-pq.pdf" "b/\350\257\276\344\273\266/lecture07-pq.pdf" index bf751be1e6720ccb13d80c752dc2587677aabb6e..ff104a93484385d71f6b62d9e979159663f0164b 100644 Binary files "a/\350\257\276\344\273\266/lecture07-pq.pdf" and "b/\350\257\276\344\273\266/lecture07-pq.pdf" differ