XJTUSE 数据结构与算法第三次作业——任务3

任务3

  1. 题目

堆是一棵完全二叉树,是二叉树众多应用中一个最适合用数组方式存储的树形,所以必须要完全掌握。堆的主要用途是构建优先队列ADT,除此之外,高效的从若干个数据中连续找到剩余元素中最小(或最大)都是堆的应用场景,比如构建 Huffman 树时,需要从候选频率中选择最小的两个;比如最短路径 Dijkstra 算法中要从最短路径估计值数组中选取当前最小值等,所有的这些应用都因为使用了堆而如虎添翼。本任务从几个方面对堆进行实践:

1) 参看书中的代码,撰写一个具有 insertdeletegetMin 等方法的 Min_Heap

2) 完成一个使用 1)编写的 Min_Heap 的排序算法;

3) 堆是二叉树的一个应用,如果将堆的概念扩展到三叉树(树中每个结点的子结点数最多是 3个),此时将形成三叉堆。除了树形和二叉堆不一致外,堆序的要求是完全一致的。使用完全三叉树形成堆,在 n 个结点下显然会降低整棵树的高度,但是在维持某个结点的堆序时需要比较的次数会增加(这个时候会是 3 个子结点之间进行互相比较求最大或最小),这也是一个权衡问题。现在要求将 1)中实现的二叉堆改造成三叉堆,并通过 2)中实现的排序验证这个三叉堆是否正确。

  1. 任务1
  1. 数据结构设计

堆是一种树堆中的每一个节点值都大于等于(或小于等于)子树中所有节点的值。或者说,任意一个节点的值都大于等于(或小于等于)所有子节点的值。根据大于或者小于区分为大顶堆和小顶堆。堆不一定是完全二叉树,只是为了方便存储和索引,我们通常用完全二叉树的形式来表示堆

相对于有序数组而言,堆的主要优势在于插入和删除数据效率较高。 因为堆是基于完全二叉树实现的,所以在插入和删除数据时,只需要在二叉树中上下移动节点,时间复杂度为 O(log(n)),相比有序数组的 O(n),效率更高。Heap 初始化的时间复杂度为 O(n),而非O(nlogn)。

我建立的小顶堆数组索引0的位置为空,这样可以更轻易找到子节点和父节点,类中包括一个 public double[] array用于存储堆元素的数组和一个public int usedSize表示堆中已使用的元素数量。还有两个构造方法,分别是外界传入数组的构造方法和一个没有传入数组的构造方法,默认容量为10,后续可以扩容。

  1. public class Min_Heap_BinaryTree {
  2.     //二叉树中的数组索引0的位置为空,这样可以更轻易找到子节点和父节点
  3.     public double[] array; // 存储堆元素的数组
  4.     public int usedSize; // 堆中已使用的元素数量
  5.     public Min_Heap_BinaryTree(double[] array) {
  6.         this.array = new double[array.length + 1];
  7.         this.usedSize = array.length; // 初始化已使用元素数量为传入数组的长度
  8.         buildHeap(array);
  9.     }
  10.     public Min_Heap_BinaryTree() {
  11.         this.array = new double[11]; // 创建长度为11的数组
  12.         this.usedSize = 0// 初始化已使用元素数量为0
  13.     }
  14. }

Insert方法接受一个double类型的值作为参数,表示要插入堆中的新元素;接着检查当前堆的容量是否不足,即usedSize + 1等于数组的长度如果容量不足,就调用resizeArray()方法进行数组的扩容接下来,将usedSize + 1作为索引,将新元素插入到堆的末尾位置。这里的usedSize + 1是因为数组索引0的位置为空,所以实际使用的索引从1开始增加usedSize的值,表示堆中已使用的元素数量增加了一个最后,调用heapifyUp(index)方法,向上调整堆。这是为了确保新插入的元素在堆中找到合适的位置并满足最小堆的性质。

heapifyUp方法接受一个索引值作为参数,表示需要向上调整的节点的索引然后,计算父节点的索引,即index除以2进入一个循环,条件是当前节点的索引大于1且当前节点的值小于其父节点的值。这是因为最小堆要求父节点的值小于等于子节点的值在循环中,首先调用swap(index, parentIndex)方法交换当前节点和父节点的值,将较小的值移动到上层位置然后,更新当前节点的索引为父节点的索引,即index = parentIndex再次计算父节点的索引,即parentIndex = index / 2重复上述步骤,直到当前节点的值大于等于其父节点的值,或者当前节点已经到达堆的根节点(索引为1)为止。

  1.     public void insert(double value) {
  2.         if (usedSize + 1 == array.length) { // 如果数组容量不足,则调整数组大小
  3.             resizeArray();
  4.         }
  5.         int index = usedSize + 1;
  6.         array[index] = value; //
  7.         usedSize++;
  8.         heapifyUp(index);
  9.     }
  10.     private void heapifyUp(int index) {
  11.         int parentIndex = index / 2;
  12.         while (index > 1 && array[index] < array[parentIndex]) {
  13.             swap(index, parentIndex);
  14.             index = parentIndex;
  15.             parentIndex = index / 2;
  16.         }
  17.     }

delete方法检查堆是否为空,即usedSize等于0如果堆为空,抛出一个IllegalStateException异常,表示堆为空,无法执行删除操作然后,将堆顶元素的值赋给minValue,即最小值将堆的最后一个元素移动到堆顶(索引为1的位置),即array[1] = array[usedSize]减少usedSize的值,表示堆中已使用的元素数量减少了一个最后,调用heapifyDown(1)方法,向下调整堆。这是为了确保新的堆顶元素找到合适的位置并满足最小堆的性质。

heapifyDown方法接受一个索引值作为参数,表示需要向下调整的节点的索引初始化smallest变量为当前节点的索引计算左子节点和右子节点的索引接着,进行一系列条件判断。首先判断左子节点的索引是否在堆范围内,并且左子节点的值是否小于当前最小值节点的值。如果满足条件,将smallest更新为左子节点的索引然后,判断右子节点的索引是否在堆范围内,并且右子节点的值是否小于当前最小值节点的值。如果满足条件,将smallest更新为右子节点的索引最后,如果smallest不等于当前节点的索引,说明需要进行交换。调用swap(index, smallest)方法交换当前节点和最小值节点的值递归调用heapifyDown(smallest)方法,以确保被交换的节点在下层继续向下调整,以满足最小堆的性质。

  1.     public double delete() {
  2.         if (usedSize == 0) {
  3.             throw new IllegalStateException("Heap is empty");
  4.         }
  5.         double minValue = array[1];
  6.         array[1] = array[usedSize]; // 将最后一个元素移动到堆顶
  7.         usedSize--;
  8.         heapifyDown(1); // 向下调整堆
  9.         return minValue;
  10.     }
  11.     private void heapifyDown(int index) {
  12.         int smallest = index;
  13.         int leftChild = 2 * index;
  14.         int rightChild = 2 * index + 1;
  15.         if (leftChild <= usedSize && array[leftChild] < array[smallest]) {
  16.             smallest = leftChild;
  17.         }
  18.         if (rightChild <= usedSize && array[rightChild] < array[smallest]) {
  19.             smallest = rightChild;
  20.         }
  21.         if (smallest != index) { // 如果最小值索引不等于当前节点索引,说明需要进行交换
  22.             swap(index, smallest);
  23.             heapifyDown(smallest); // 递归向下调整堆
  24.         }
  25.     }

buildHeap方法接受一个double类型的数组initialArray作为参数,表示初始数组使用一个循环,将初始数组的元素复制到堆数组中接下来,使用另一个循环,从最后一个非叶子节点开始向下调整堆。循环的起始索引是usedSize / 2在循环中,调用heapifyDown(i)方法,向下调整堆。这是为了确保从当前节点开始的子树满足最小堆的性质。

  1.     public double getMin() {
  2.         if (usedSize == 0) { // 如果堆为空,则抛出异常
  3.             throw new IllegalStateException("Heap is empty");
  4.         }
  5.         return array[1]; // 返回堆顶元素,即最小值
  6.     }
  7.     private void buildHeap(double[] initialArray) {
  8.         for (int i = 0; i < usedSize; i++) {
  9.             array[i + 1] = initialArray[i]; // 将传入数组的元素复制到堆数组中
  10.         }
  11.         for (int i = (usedSize / 2); i >= 1; i--) {
  12.             heapifyDown(i); // 从最后一个非叶子节点开始向下调整堆
  13.         }
  14.     }

两个私有方法比较简单,不再赘述。

  1.     private void swap(int i, int j) {
  2.         double temp = array[i];
  3.         array[i] = array[j];
  4.         array[j] = temp;
  5.     }
  6.     private void resizeArray() {
  7.         double[] newArray= new double[array.length * 2]; // 创建新的数组,长度是原数组的两倍
  8.         System.arraycopy(array, 0, newArray, 0, array.length); // 将原数组的元素复制到新数组中
  9.         array = newArray; // 更新数组引用为新数组
  10.     }

  1. 测试

测试代码如下:

  1. package Homework03;
  2. public class Min_HeapTest_BinaryTree {
  3.     public static void main(String[] args) {
  4.         // 创建一个空的小堆
  5.         Min_Heap_BinaryTree heap = new Min_Heap_BinaryTree();
  6.         // 插入元素
  7.         heap.insert(5);
  8.         heap.insert(3);
  9.         heap.insert(8);
  10.         heap.insert(1);
  11.         heap.insert(10);
  12.         // 获取最小值
  13.         System.out.println("最小值:" + heap.getMin()); // 1
  14.         // 删除最小值
  15.         double minValue = heap.delete();
  16.         System.out.println("删除的最小值:" + minValue); // 1
  17.         // 再次获取最小值
  18.         System.out.println("最小值:" + heap.getMin()); // 3
  19.         // 打印堆中剩余元素
  20.         System.out.println("堆中剩余元素:");
  21.         while (heap.usedSize > 0) {
  22.             double value = heap.delete();
  23.             System.out.println(value);
  24.         }
  25.     }
  26. }

  1. 测试结果:

图4  二叉树构建的小顶堆文件测试结果

  1. 任务2
  1. 堆排序算法

堆排序算法很简单,创建一个Min_Heap_BinaryTree对象heap,并将待排序的数组传递给它。使用一个循环,从数组的末尾开始,依次将堆顶元素删除,并将其放入已排序部分的末尾。循环的起始索引是n - 1,循环条件是i >= 0,每次循环结束后,将i减1。在循环中,调用heap.delete()方法,该方法会删除堆顶元素并返回该元素的值。将返回的值赋给数组中对应的位置。

  1.     public static void heapSort(double[] array) {
  2.         int n = array.length;
  3.         // 构建小堆
  4.         Min_Heap_BinaryTree heap = new Min_Heap_BinaryTree(array);
  5.         // 依次删除堆顶元素,放入已排序部分的末尾
  6.         for (int i = n - 1; i >= 0; i--) {
  7.             array[i] = heap.delete();
  8.         }
  9.     }

  1. 测试
  1.     public static void main(String[] args) {
  2.         double[] array = { 410659 };
  3.         System.out.println("排序前:");
  4.         printArray(array);
  5.         heapSort(array);
  6.         System.out.println("排序后:");
  7.         printArray(array);
  8.     }

  1. 测试结果:

图5 二叉树构建的小顶堆堆排序结果

  1. 任务3
  1. 数据结构设计

二叉树构建的堆和三叉树构建的堆其实有很多相似的地方,区别有如下几个:

子结点位置:在二叉树构建的堆中,每个结点有左子结点和右子结点。左子结点在数组中的索引位置为2i-1,右子结点在数组中的索引位置为2i,其中i是父结点在数组中的索引位置。而在三叉树构建的堆中,每个结点有左子结点、中子结点和右子结点。左子结点在数组中的索引位置为3i-1,中子结点在数组中的索引位置为3i,右子结点在数组中的索引位置为3i+1,其中i是父结点在数组中的索引位置。

向下调整堆的方式:在二叉树构建的堆中,我们需要比较父节点和两个子节点,考虑是否交换位置以及和哪个子节点交换。在三叉树构建的堆中,我们需要比较四个节点,考虑是否交换位置以及和哪个子节点交换。

三叉树构建的堆的详细代码贴在附录,这里仅仅列出向下调整堆的方法。

  1.     private void heapifyDown(int index) {
  2.         int child1Index = 3 * index - 1;
  3.         int child2Index = 3 * index;
  4.         int child3Index = 3 * index + 1;
  5.         int smallestChildIndex = index;
  6.         if (child1Index <= usedSize && array[child1Index] < array[smallestChildIndex]) {
  7.             smallestChildIndex = child1Index;
  8.         }
  9.         if (child2Index <= usedSize && array[child2Index] < array[smallestChildIndex]) {
  10.             smallestChildIndex = child2Index;
  11.         }
  12.         if (child3Index <= usedSize && array[child3Index] < array[smallestChildIndex]) {
  13.             smallestChildIndex = child3Index;
  14.         }
  15.         if (smallestChildIndex != index) {
  16.             swap(index, smallestChildIndex);
  17.             heapifyDown(smallestChildIndex);
  18.         }
  19.     }

heapifyDown方法接受一个索引index作为参数,表示需要进行向下调整的结点在数组中的索引位置。根据三叉树的性质,计算出当前结点的三个子结点的索引位置,然后初始化smallestChildIndex变量为当前结点的索引,表示当前结点是目前最小的结点通过一系列的条件判断,找到当前结点的三个子结点中值最小的子结点,并将其索引赋给smallestChildIndex如果smallestChildIndex与index不相等,说明最小子结点的值小于当前结点的值,需要进行交换递归调用heapifyDown方法,以最小子结点的索引作为参数,继续向下调整堆。

使用三叉树构建的小顶堆进行堆排序很简单,类似上一问。

  1.     public static void heapSort(double[] array) {
  2.         int n = array.length;
  3.         // 构建小堆
  4.         Min_Heap_TernaryTree heap = new Min_Heap_TernaryTree(array);
  5.         // 依次删除堆顶元素,放入已排序部分的末尾
  6.         for (int i = n - 1; i >= 0; i--) {
  7.             array[i] = heap.delete();
  8.         }
  9.     }

  1. 测试

测试三叉树构建的小顶堆的方法的代码:

  1. package Homework03;
  2. public class Min_HeapTernaryTreeTest {
  3.     public static void main(String[] args) {
  4.         // 创建一个空的小堆
  5.         Min_Heap_TernaryTree heap = new Min_Heap_TernaryTree();
  6.         // 插入元素
  7.         heap.insert(5);
  8.         heap.insert(3);
  9.         heap.insert(8);
  10.         heap.insert(1);
  11.         heap.insert(10);
  12.         // 获取最小值
  13.         System.out.println("最小值:" + heap.getMin()); // 1.0
  14.         // 删除最小值
  15.         double minValue = heap.delete();
  16.         System.out.println("删除的最小值:" + minValue); // 1.0
  17.         // 再次获取最小值
  18.         System.out.println("最小值:" + heap.getMin()); // 3.0
  19.         // 打印堆中剩余元素
  20.         System.out.println("堆中剩余元素:");
  21.         while (heap.usedSize > 0) {
  22.             double value = heap.delete();
  23.             System.out.println(value);
  24.         }
  25.     }
  26. }

测试利用三叉树构建的小顶堆进行堆排序的代码:

  1.     public static void main(String[] args) {
  2.         double[] array = { 410659 };
  3.         System.out.println("排序前:");
  4.         printArray(array);
  5.         heapSort(array);
  6.         System.out.println("排序后:");
  7.         printArray(array);
  8.     }

  1. 测试结果:

图6 三叉树构建的小顶堆测试结果

图7 三叉树构建的小顶堆堆排序结果

  1. 总结和收获

复习了堆以及堆排序的算法,收获很大。

附录:

  1. 任务3
  1. Min_Heap_BinaryTree:
  1. package Homework03;
  2. public class Min_Heap_BinaryTree {
  3.     //二叉树中的数组索引0的位置为空,这样可以更轻易找到子节点和父节点
  4.     public double[] array; // 存储堆元素的数组
  5.     public int usedSize; // 堆中已使用的元素数量
  6.     public Min_Heap_BinaryTree(double[] array) {
  7.         this.array = new double[array.length + 1];
  8.         this.usedSize = array.length; // 初始化已使用元素数量为传入数组的长度
  9.         buildHeap(array);
  10.     }
  11.     public Min_Heap_BinaryTree() {
  12.         this.array = new double[11]; // 创建长度为11的数组
  13.         this.usedSize = 0// 初始化已使用元素数量为0
  14.     }
  15.     public void insert(double value) {
  16.         if (usedSize + 1 == array.length) { // 如果数组容量不足,则调整数组大小
  17.             resizeArray();
  18.         }
  19.         int index = usedSize + 1;
  20.         array[index] = value; //
  21.         usedSize++;
  22.         heapifyUp(index);
  23.     }
  24.     public double delete() {
  25.         if (usedSize == 0) {
  26.             throw new IllegalStateException("Heap is empty");
  27.         }
  28.         double minValue = array[1];
  29.         array[1] = array[usedSize]; // 将最后一个元素移动到堆顶
  30.         usedSize--;
  31.         heapifyDown(1); // 向下调整堆
  32.         return minValue;
  33.     }
  34.     public double getMin() {
  35.         if (usedSize == 0) { // 如果堆为空,则抛出异常
  36.             throw new IllegalStateException("Heap is empty");
  37.         }
  38.         return array[1]; // 返回堆顶元素,即最小值
  39.     }
  40.     private void buildHeap(double[] initialArray) {
  41.         for (int i = 0; i < usedSize; i++) {
  42.             array[i + 1] = initialArray[i]; // 将传入数组的元素复制到堆数组中
  43.         }
  44.         for (int i = (usedSize / 2); i >= 1; i--) {
  45.             heapifyDown(i); // 从最后一个非叶子节点开始向下调整堆
  46.         }
  47.     }
  48.     private void heapifyUp(int index) {
  49.         int parentIndex = index / 2;
  50.         while (index > 1 && array[index] < array[parentIndex]) {
  51.             swap(index, parentIndex);
  52.             index = parentIndex;
  53.             parentIndex = index / 2;
  54.         }
  55.     }
  56.     private void heapifyDown(int index) {
  57.         int smallest = index;
  58.         int leftChild = 2 * index;
  59.         int rightChild = 2 * index + 1;
  60.         if (leftChild <= usedSize && array[leftChild] < array[smallest]) {
  61.             smallest = leftChild;
  62.         }
  63.         if (rightChild <= usedSize && array[rightChild] < array[smallest]) {
  64.             smallest = rightChild;
  65.         }
  66.         if (smallest != index) { // 如果最小值索引不等于当前节点索引,说明需要进行交换
  67.             swap(index, smallest);
  68.             heapifyDown(smallest); // 递归向下调整堆
  69.         }
  70.     }
  71.     private void swap(int i, int j) {
  72.         double temp = array[i];
  73.         array[i] = array[j];
  74.         array[j] = temp;
  75.     }
  76.     private void resizeArray() {
  77.         double[] newArray= new double[array.length * 2]; // 创建新的数组,长度是原数组的两倍
  78.         System.arraycopy(array, 0, newArray, 0, array.length); // 将原数组的元素复制到新数组中
  79.         array = newArray; // 更新数组引用为新数组
  80.     }
  81. }

  1. Min_Heap_TernaryTree:
  1. package Homework03;
  2. public class Min_Heap_TernaryTree {
  3.     public double[] array;
  4.     public int usedSize;
  5.     public Min_Heap_TernaryTree(double[] array) {
  6.         this.array = new double[array.length + 1];
  7.         this.usedSize = array.length;
  8.         buildHeap(array);
  9.     }
  10.     public Min_Heap_TernaryTree() {
  11.         this.array = new double[11];
  12.         this.usedSize = 0;
  13.     }
  14.     public void insert(double value) {
  15.         if (usedSize + 1 == array.length) {
  16.             resizeArray();
  17.         }
  18.         int index = usedSize + 1;
  19.         array[index] = value;
  20.         usedSize++;
  21.         heapifyUp(index);
  22.     }
  23.     public double delete() {
  24.         if (usedSize == 0) {
  25.             throw new IllegalStateException("Heap is empty");
  26.         }
  27.         double minValue = array[1];
  28.         array[1] = array[usedSize];
  29.         usedSize--;
  30.         heapifyDown(1);
  31.         return minValue;
  32.     }
  33.     public double getMin() {
  34.         if (usedSize == 0) {
  35.             throw new IllegalStateException("Heap is empty");
  36.         }
  37.         return array[1];
  38.     }
  39.     private void buildHeap(double[] initialArray) {
  40.         for (int i = 0; i < usedSize; i++) {
  41.             array[i + 1] = initialArray[i];
  42.         }
  43.         for (int i = usedSize / 3; i >= 1; i--) {
  44.             heapifyDown(i);
  45.         }
  46.     }
  47.     private void heapifyUp(int index) {
  48.         int parentIndex = (index - 1) / 3;
  49.         while (index > 1 && array[index] < array[parentIndex]) {
  50.             swap(index, parentIndex);
  51.             index = parentIndex;
  52.             parentIndex = (index - 1) / 3;
  53.         }
  54.     }
  55.     private void heapifyDown(int index) {
  56.         int child1Index = 3 * index - 1;
  57.         int child2Index = 3 * index;
  58.         int child3Index = 3 * index + 1;
  59.         int smallestChildIndex = index;
  60.         if (child1Index <= usedSize && array[child1Index] < array[smallestChildIndex]) {
  61.             smallestChildIndex = child1Index;
  62.         }
  63.         if (child2Index <= usedSize && array[child2Index] < array[smallestChildIndex]) {
  64.             smallestChildIndex = child2Index;
  65.         }
  66.         if (child3Index <= usedSize && array[child3Index] < array[smallestChildIndex]) {
  67.             smallestChildIndex = child3Index;
  68.         }
  69.         if (smallestChildIndex != index) {
  70.             swap(index, smallestChildIndex);
  71.             heapifyDown(smallestChildIndex);
  72.         }
  73.     }
  74.     private void swap(int i, int j) {
  75.         double temp = array[i];
  76.         array[i] = array[j];
  77.         array[j] = temp;
  78.     }
  79.     private void resizeArray() {
  80.         double[] newArray = new double[array.length * 2];
  81.         System.arraycopy(array, 0, newArray, 0, array.length);
  82.         array = newArray;
  83.     }
  84. }

  1. Min_HeapTest_BinaryTree:
  1. package Homework03;
  2. public class Min_HeapTest_BinaryTree {
  3.     public static void main(String[] args) {
  4.         // 创建一个空的小堆
  5.         Min_Heap_BinaryTree heap = new Min_Heap_BinaryTree();
  6.         // 插入元素
  7.         heap.insert(5);
  8.         heap.insert(3);
  9.         heap.insert(8);
  10.         heap.insert(1);
  11.         heap.insert(10);
  12.         // 获取最小值
  13.         System.out.println("最小值:" + heap.getMin()); // 1
  14.         // 删除最小值
  15.         double minValue = heap.delete();
  16.         System.out.println("删除的最小值:" + minValue); // 1
  17.         // 再次获取最小值
  18.         System.out.println("最小值:" + heap.getMin()); // 3
  19.         // 打印堆中剩余元素
  20.         System.out.println("堆中剩余元素:");
  21.         while (heap.usedSize > 0) {
  22.             double value = heap.delete();
  23.             System.out.println(value);
  24.         }
  25.     }
  26. }

  1. Min_HeapTernaryTreeTest:
  1. package Homework03;
  2. public class Min_HeapTernaryTreeTest {
  3.     public static void main(String[] args) {
  4.         // 创建一个空的小堆
  5.         Min_Heap_TernaryTree heap = new Min_Heap_TernaryTree();
  6.         // 插入元素
  7.         heap.insert(5);
  8.         heap.insert(3);
  9.         heap.insert(8);
  10.         heap.insert(1);
  11.         heap.insert(10);
  12.         // 获取最小值
  13.         System.out.println("最小值:" + heap.getMin()); // 1.0
  14.         // 删除最小值
  15.         double minValue = heap.delete();
  16.         System.out.println("删除的最小值:" + minValue); // 1.0
  17.         // 再次获取最小值
  18.         System.out.println("最小值:" + heap.getMin()); // 3.0
  19.         // 打印堆中剩余元素
  20.         System.out.println("堆中剩余元素:");
  21.         while (heap.usedSize > 0) {
  22.             double value = heap.delete();
  23.             System.out.println(value);
  24.         }
  25.     }
  26. }

  1. HeapSortBinaryTree:
  1. package Homework03;
  2. public class HeapSortBinaryTree {
  3.     public static void heapSort(double[] array) {
  4.         int n = array.length;
  5.         // 构建小堆
  6.         Min_Heap_BinaryTree heap = new Min_Heap_BinaryTree(array);
  7.         // 依次删除堆顶元素,放入已排序部分的末尾
  8.         for (int i = n - 1; i >= 0; i--) {
  9.             array[i] = heap.delete();
  10.         }
  11.     }
  12.     public static void printArray(double[] array) {
  13.         for (double num : array) {
  14.             System.out.print(num + " ");
  15.         }
  16.         System.out.println();
  17.     }
  18.     public static void main(String[] args) {
  19.         double[] array = { 410659 };
  20.         System.out.println("排序前:");
  21.         printArray(array);
  22.         heapSort(array);
  23.         System.out.println("排序后:");
  24.         printArray(array);
  25.     }
  26. }

  1. HeapSortTernaryTree:
  1. package Homework03;
  2. public class HeapSortTernaryTree {
  3.     public static void heapSort(double[] array) {
  4.         int n = array.length;
  5.         // 构建小堆
  6.         Min_Heap_TernaryTree heap = new Min_Heap_TernaryTree(array);
  7.         // 依次删除堆顶元素,放入已排序部分的末尾
  8.         for (int i = n - 1; i >= 0; i--) {
  9.             array[i] = heap.delete();
  10.         }
  11.     }
  12.     public static void printArray(double[] array) {
  13.         for (double num : array) {
  14.             System.out.print(num + " ");
  15.         }
  16.         System.out.println();
  17.     }
  18.     public static void main(String[] args) {
  19.         double[] array = { 410659 };
  20.         System.out.println("排序前:");
  21.         printArray(array);
  22.         heapSort(array);
  23.         System.out.println("排序后:");
  24.         printArray(array);
  25.     }
  26. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值