数据结构和算法16 之堆排序

简介:

   堆排序,顾名思义就是利用堆这个数据结构对数据项进行排序,前面提到过,堆数据结构中,节点大于或等于自己的子节点。那么我们可以将待排序的数据项依次添加到堆中,然后再依次取出根节点即可。从堆中取出的数据项是从大到小排列的。因为根节点永远是最大的,而堆中永远是取根节点。如果对堆这种数据结构不太了解的话,可以先看这篇博文:数据结构和算法之 堆,这里不再赘述。

下面我们来看看堆排序的实现(如果程序有不清楚的地方,也可以参考上面那篇博文)。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class HeapSort {  
  2.     private int[] array;  
  3.     private int currentIndex;  
  4.     private int maxSize;  
  5.     public HeapSort(int size) {  
  6.         maxSize = size;  
  7.         array = new int[maxSize];  
  8.         currentIndex = 0;  
  9.     }  
  10.     //插入数据项,并排好序  
  11.     public void insertSort(int[] source) {  
  12.         for(int i = 0; i < source.length; i++) {  
  13.             array[currentIndex] = source[i]; //插入到节点尾  
  14.             tickedUp(currentIndex++); //向上重新排好序,使得满足堆的条件  
  15.         }  
  16.     }  
  17.     private void tickedUp(int index) {  
  18.         int parentIndex = (index - 1) / 2//父节点的索引  
  19.         int temp = array[index]; //将新加的尾节点存在temp中  
  20.         while(index > 0 && array[parentIndex] < temp) {  
  21.             array[index] = array[parentIndex];  
  22.             index = parentIndex;  
  23.             parentIndex = (index - 1) / 2;  
  24.         }  
  25.         array[index] = temp;  
  26.     }  
  27.       
  28.     //取出最大值  
  29.     public int getMax() {  
  30.         int maxNum = array[0];  
  31.         array[0] = array[--currentIndex];  
  32.         trickleDown(0);  
  33.         return maxNum;  
  34.     }  
  35.     private void trickleDown(int index) {  
  36.         int top = array[index];  
  37.         int largeChildIndex;  
  38.         while(index < currentIndex/2) { //while node has at least one child  
  39.             int leftChildIndex = 2 * index + 1;  
  40.             int rightChildIndex = leftChildIndex + 1;  
  41.             //find larger child  
  42.             if(rightChildIndex < currentIndex &&  //rightChild exists?  
  43.                     array[leftChildIndex] < array[rightChildIndex]) {  
  44.                 largeChildIndex = rightChildIndex;  
  45.             }  
  46.             else {  
  47.                 largeChildIndex = leftChildIndex;  
  48.             }  
  49.             if(top >= array[largeChildIndex]) {  
  50.                 break;  
  51.             }  
  52.             array[index] = array[largeChildIndex];  
  53.             index = largeChildIndex;  
  54.         }  
  55.         array[index] = top;  
  56.     }  
  57. }  

        算法分析:堆中插入和取出的时间复杂度均为O(logN),所以堆排序算法的时间复杂度为O(NlogN),但是堆排序也需要额外的和待排序序列大小相同的存储空间。空间复杂度为O(N)。


转载:http://blog.csdn.net/eson_15/article/details/51193399

目录
相关文章
|
16天前
|
存储 算法 索引
【算法与数据结构】队列的实现详解
【算法与数据结构】队列的实现详解
|
20天前
|
算法
【算法与数据结构】二叉树(前中后)序遍历2
【算法与数据结构】二叉树(前中后)序遍历
|
6天前
|
算法
堆排序+TopK问题——“数据结构与算法”
堆排序+TopK问题——“数据结构与算法”
|
8天前
|
存储 机器学习/深度学习 算法
上机实验三 图的最小生成树算法设计 西安石油大学数据结构
上机实验三 图的最小生成树算法设计 西安石油大学数据结构
17 1
|
16天前
|
算法 索引
【算法与数据结构】深入二叉树实现超详解(全源码优化)
【算法与数据结构】深入二叉树实现超详解(全源码优化)
|
16天前
|
存储 算法
【算法与数据结构】深入解析二叉树(二)之堆结构实现
【算法与数据结构】深入解析二叉树(二)之堆结构实现
|
20天前
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解
|
20天前
|
存储 算法 C语言
【算法与数据结构】 C语言实现单链表队列详解1
【算法与数据结构】 C语言实现单链表队列详解
|
30天前
|
传感器 算法 计算机视觉
基于肤色模型和中值滤波的手部检测算法FPGA实现,包括tb测试文件和MATLAB辅助验证
该内容是关于一个基于肤色模型和中值滤波的手部检测算法的描述,包括算法的运行效果图和所使用的软件版本(matlab2022a, vivado2019.2)。算法分为肤色分割和中值滤波两步,其中肤色模型在YCbCr色彩空间定义,中值滤波用于去除噪声。提供了一段核心程序代码,用于处理图像数据并在FPGA上实现。最终,检测结果输出到&quot;hand.txt&quot;文件。
|
1月前
|
机器学习/深度学习 算法 计算机视觉
基于yolov2深度学习网络的视频手部检测算法matlab仿真
基于yolov2深度学习网络的视频手部检测算法matlab仿真