MATLAB实现堆排序算法

时间: 2023-11-07 18:24:31 浏览: 40
function heapSort(A) %HEAPSORT Sorts an array in ascending order using the heap sort algorithm. % A: input array to be sorted % Get the length of the array n = length(A); % Build a max heap using the input array for i = floor(n/2):-1:1 A = maxHeapify(A, i, n); end % Extract the maximum element from the heap and place it at the end of the % array. Then rebuild the heap with the remaining elements. for i = n:-1:2 temp = A(1); A(1) = A(i); A(i) = temp; n = n - 1; A = maxHeapify(A, 1, n); end % Display the sorted array disp(A); end function A = maxHeapify(A, i, n) %MAXHEAPIFY Maintains the max heap property by recursively adjusting the %elements in the heap. % A: input array that represents the heap % i: index of the element to be adjusted % n: size of the heap % Get the left and right child indices of the element at index i left = 2*i; right = 2*i + 1; % Find the index of the largest element among A(i), A(left), and A(right) if left <= n && A(left) > A(i) largest = left; else largest = i; end if right <= n && A(right) > A(largest) largest = right; end % If the largest element is not the element at index i, swap the two elements % and recursively adjust the elements in the heap. if largest ~= i temp = A(i); A(i) = A(largest); A(largest) = temp; A = maxHeapify(A, largest, n); end end

相关推荐

最新推荐

recommend-type

用fft算法实现相关的MATLAB仿真

用fft算法实现相关的MATLAB仿真,该方法易于在FPGA上实现相关算法,比直接用相乘来得简单,而且但相关点数越多计算量相对而言比直接求解减少
recommend-type

MATLAB 智能算法30个案例分析与详解

包含全部MATLAB程序 遗传算法中常用函数 1. 创建种群函数—crtbp 2. 适应度计算函数—ranking 3. 选择函数—select 4. 交叉算子函数—recombin 5. 变异算子函数—mut 6. 选择函数—reins 7. 实用函数—bs2rv 8. 实用...
recommend-type

Matlab数学建模算法全收录.pdf

数学建模算法,包括数学规划,图论,排队论,层次分析,多元统计分析方法,微分方程,模糊数学,灰色模型,神经网络,现代算法,非常全的数学建模资料,还包含相应的matlab程序,全本。
recommend-type

Kruskal算法的MATLAB实现

Kruskal算法的MATLAB实现,输入参数d是原图的权值矩阵;输出参数T是最小生成树的顶点组成的矩阵,每条边的两个顶点放在同一列中;a是最小生成树的总权值
recommend-type

census算法matlab程序

census算法matlab程序,双目立体匹配利用汉明氏距离计算匹配窗口,整体算法光敏性较好
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。