堆排序heapSort C++实现

news/2024/5/19 3:08:22 标签: 堆排序, 快速排序, 数据结构, 排序算法, 算法
#include <iostream> 
using namespace std; 
  
// 对arr[i]为根的子树建堆;i:根节点下标 n:堆大小
void heapify(int arr[], int n, int i) 
{ 
    int largest = i; // Initialize largest as root 
    int l = 2*i + 1; // left = 2*i + 1 
    int r = 2*i + 2; // right = 2*i + 2 
  
    // If left child is larger than root 
    if (l < n && arr[l] > arr[largest]) 
        largest = l; 
  
    // If right child is larger than largest so far 
    if (r < n && arr[r] > arr[largest]) 
        largest = r; 
  
    // If largest is not root 
    if (largest != i) 
    { 
        swap(arr[i], arr[largest]); 
  
        // Recursively heapify the affected sub-tree 
        heapify(arr, n, largest); 
    } 
} 
  
// main function to do heap sort 
void heapSort(int arr[], int n) 
{ 
    // Build heap (rearrange array) 
    for (int i = n / 2 - 1; i >= 0; i--) 
        heapify(arr, n, i); 
        
    // One by one extract an element from heap 
    for (int i=n-1; i>0; i--) 
    { 
        // Move current root to end 
        swap(arr[0], arr[i]); 
        // call max heapify on the reduced heap 
        heapify(arr, i, 0); 
    } 
} 

void printArray(int arr[], int n) 
{ 
    for (int i=0; i<n; ++i) 
        cout << arr[i] << " "; 
    cout << "\n"; 
} 

int main() 
{ 
    int arr[] = {12, 11, 13, 5, 6, 7}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    heapSort(arr, n); 
  
    cout << "Sorted array is \n"; 
    printArray(arr, n); 
} 

堆排序对相同长度的数组比较次数是固定的,时间性能是稳定的O(nlogn)。

但缺点也同样在此,哪怕对于一个有序的数组,堆排序要耗费同样的时间排序。

算法导论》上讲基数排序还有这么一句话:快速排序通常比基数排序更有效地使用硬件的缓存。 因为快排是顺着扫的,堆排序是跳着走的;快排的存取模型的局部性(locality)更强,,堆排序差一些。

所以大多情况下,快排要比堆排序更快。


http://www.niftyadmin.cn/n/905633.html

相关文章

systick运用

systick的原理前一篇博文有介绍&#xff0c;简而言之就是SysTick定时器是一个24位的倒计数&#xff0c;当倒计数为0时&#xff0c;将从RELOAD寄存器中取值作为定时器的初始值&#xff0c;同时可以选择在这个时候产生中断(异常号:15)。例如从RELOAD的值为999&#xff0c;那么当倒…

网维无盘服务器错误代码,网维大师无盘环境INTER傲腾方案常见问题解答?

傲腾方案使用方法&#xff1a;9060.6月5日之前版本1、联系客服索要专用驱动文件&#xff0c;替换到服务端目录*:\Program Files\网维大师\Diskless\PNP下&#xff0c;先将原文件备份一份&#xff0c;再替换客服给的vdiskbus64.sys&#xff0c;替换后要结束ControlServer.exe(必…

PAT甲级真题 1100 Mars Numbers (20分) C++实现

题目 People on Mars count their numbers with base 13: Zero on Earth is called “tret” on Mars. The numbers 1 to 12 on Earth is called “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” on Mars, respectively. For the next higher digit, Mars peo…

linetime.css,基于ccs3的timeline时间线实现方法

在web项目中我们经常要使用时间轴(timeline)控件。本文提供一种基于CSS3的可逐项展开的timeline&#xff0c;在各展开项中可以嵌入文本、图片、视频等元素。运行效果如下&#xff1a;实现该控件的实现过程较为简单&#xff0c;主要由test.html文件和timeline.css文件组成。具体…

PAT甲级真题 1101 Quick Sort (25分) C++实现 (快排基准位置,与有序数列位置相同是必要非充分条件)

题目 There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Gi…

010 注册web组件

一 .概述 在web环境之中,我们需要使用servlet,filter,listener三个基本组件. 二 .使用springboot的方式添加组件 ServletRegistrationBean: 我们可以向容器之中添加一个bean,类型为上述的类型,这个就注册了一个servlet. FilterRegistrationBean : 和上面的一样,通过这种方式就能…

$.ajax 其他域名,通过js(ajax)请求另外一个域名的接口时会产生跨域问题解决办法...

如果接口是php语言&#xff1a;header("Access-Control-Allow-Origin: *");$name isset($_POST[name])? $_POST[name] : ;$gender isset($_POST[gender])? $_POST[gender] : ;$filename time().substr($_FILES[photo][name], strrpos($_FILES[photo][name],.));…

PAT甲级真题 1102 Invert a Binary Tree (25分) C++实现(找树根,层序、中序遍历翻转二叉树)

题目 The following is from Max Howell twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off. Now it’s your turn to prove that YOU CAN invert a binary tree! Input Specif…