成都网站建设新闻,系部网站建设需求分析运行需求,模板网站定制,社区类网站开发一、堆排序算法
基本思想
堆排序是一种比较有效的排序方法#xff0c;其基本思想是#xff1a;
构建最大堆#xff1a;首先将待排序的数组构建成一个最大堆#xff0c;即对于每个非叶子节点#xff0c;它的值都大于或等于其子节点的值。排序#xff1a;然后将堆顶元素…一、堆排序算法
基本思想
堆排序是一种比较有效的排序方法其基本思想是
构建最大堆首先将待排序的数组构建成一个最大堆即对于每个非叶子节点它的值都大于或等于其子节点的值。排序然后将堆顶元素最大值与堆的最后一个元素交换位置将其移出堆并调整剩余元素以保持最大堆的性质。
步骤
构建最大堆从最后一个非叶子节点开始逐个调整子树使之满足最大堆的条件。排序重复以下操作直到堆为空 将堆顶元素最大值与堆的最后一个元素交换位置。重新调整剩余元素以保持最大堆的性质。
示例
假设我们有一个数组 [5, 2, 4, 6, 1, 3]
构建最大堆 [5, 2, 4, 6, 1, 3] - [6, 5, 4, 2, 1, 3]排序 将最大的元素 6 移动到数组的末尾然后重新调整剩余元素以保持最大堆的性质。重复此过程直到所有元素都被排序。
性能分析
时间复杂度O(n log n)其中 n 是数组中的元素数量。空间复杂度O(1)原地排序。
二、代码
#include stdlib.h
#include stdio.h
#include time.h// 函数声明
int* create_and_generate_random_array(int size);
void print_array(int *array, int size);
void heapify(int *array, int n, int i);
void heap_sort(int *array, int size);
int generate_random_size();int main() {int size generate_random_size(); // 随机生成数组大小int *array create_and_generate_random_array(size);if (array NULL) {// 如果内存分配失败printf(Memory allocation failed\n);free(array);return 1;}// 打印原始数组如果需要可以取消注释// printf(Original array:\n);// print_array(array, size);// 获取开始时间clock_t start_time clock();// 对数组进行堆排序heap_sort(array, size);// 获取结束时间clock_t end_time clock();// 计算时间差并转换为毫秒double execution_time ((double)(end_time - start_time) / CLOCKS_PER_SEC) * 1000;// 打印排序后的数组如果需要可以取消注释// printf(Sorted array:\n);// print_array(array, size);printf(array_size %d\n, size);// 打印执行时间printf(Execution time: %.2f ms\n, execution_time);// 释放分配的内存free(array);return 0;
}// 生成随机数组大小
int generate_random_size() {srand(time(NULL));return rand() % 9000 1000; // 生成1000到9999之间的随机数
}// 创建并生成随机数组
int* create_and_generate_random_array(int size) {int *array (int *)malloc(sizeof(int) * size);if (array NULL) {// 如果内存分配失败return NULL;}// 使用当前时间作为随机数种子srand(time(NULL));for (int i 0; i size; i) {array[i] rand() % 1000; // 生成0到999之间的随机数}return array;
}// 打印数组
void print_array(int *array, int size) {for (int i 0; i size; i) {printf(%d , array[i]);}printf(\n);
}// 构建最大堆
void heapify(int *array, int n, int i) {int largest i; // 初始化最大值索引int left 2 * i 1; // 左子节点int right 2 * i 2; // 右子节点// 如果左子节点大于根if (left n array[left] array[largest])largest left;// 如果右子节点大于当前最大值if (right n array[right] array[largest])largest right;// 如果最大值不是根if (largest ! i) {int swap array[i];array[i] array[largest];array[largest] swap;// 递归地堆化受影响的子树heapify(array, n, largest);}
}// 堆排序
void heap_sort(int *array, int size) {// 构建最大堆for (int i size / 2 - 1; i 0; i--)heapify(array, size, i);// 一个接一个从堆顶取出元素for (int i size - 1; i 0; i--) {// 将当前根最大值移动到数组末尾int temp array[0];array[0] array[i];array[i] temp;// 调整剩余堆使其成为最大堆heapify(array, i, 0);}
}
文章转载自: http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.gqddl.cn.gov.cn.gqddl.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.rdsst.cn.gov.cn.rdsst.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn