堆排序(heap sort)
来源:互联网 发布:软件外包接单 编辑:程序博客网 时间:2024/06/12 00:11
核心思想: 使用最大堆,每次从堆顶取出当前最大元素,并与堆末尾元素替换,并且把当前堆大小减少1,进行n-1次后 排序完成。
时间复杂度: o(NlogN)
核心代码:
//如果数组从0开始则取不到size 下面的 child != size-1 (关键) 若写成child != size 则越界
//如果数组从1开始存数据 则要取到size 下面相应写成child != size
void adjust_heap(Heap *h, int i, int size){int child, parent;int tmp = h->data[i]; //整个过程就是下滤操作 不断调整堆for(parent = i; parent*2 + 1 < size; parent = child){ child = parent*2+1;if(child != size-1 && h->data[child] < h->data[child+1])child++;if(h->data[child] > tmp){h->data[parent] = h->data[child];}elsebreak;} h->data[parent] = tmp;}void heap_sort(Heap *h, int n){int i;for(i = n/2-1; i >= 0; i--){ //i的初始值与数组存元素的开始索引有关 (调整为最大堆的过程)adjust_heap(h, i, n);}for(i = n-1; i > 0; i--){ //最后剩下的一个是最小的元素 swap(&h->data[0], &h->data[i]);adjust_heap(h, 0, i);}printf("排序后:\n");for(i = 0; i < h->size; i++ ){printf("%d ",h->data[i]);}}
完整实现:
#include<stdio.h>#include<stdlib.h>#define MAX 100typedef struct HeapNode{int size;int data[MAX];}Heap;void init_heap(Heap *h){h->size = 0;}void build_heap(Heap *h){int n;scanf("%d",&n);h->size = n;int i;printf("排序前:\n");for(i = 0; i < n; i++ ){scanf("%d",&h->data[i]);}}void swap(int *a, int *b){int temp;temp = *a;*a = *b; *b = temp;}void adjust_heap(Heap *h, int i, int size){int child, parent;int tmp = h->data[i];for(parent = i; parent*2 + 1 < size; parent = child){ child = parent*2+1;if(child != size-1 && h->data[child] < h->data[child+1])child++;if(h->data[child] > tmp){h->data[parent] = h->data[child];}elsebreak;} h->data[parent] = tmp;}void heap_sort(Heap *h, int n){int i;for(i = n/2-1; i >= 0; i--){ //i的初始值与数组存元素的开始索引有关 adjust_heap(h, i, n);}for(i = n-1; i > 0; i--){ //最后剩下的一个是最小的元素 swap(&h->data[0], &h->data[i]);adjust_heap(h, 0, i);}printf("排序后:\n");for(i = 0; i < h->size; i++ ){printf("%d ",h->data[i]);}}int main(){int i;Heap h;init_heap(&h);build_heap(&h);heap_sort(&h,h.size);return 0;}
阅读全文
1 0
- 堆排序(heap sort)
- 堆排序(Heap Sort)
- 堆排序(Heap-Sort)
- 堆排序(heap sort)
- 堆排序(heap sort)
- 堆排序(Heap Sort)
- 堆排序(heap Sort)
- 堆排序(Heap Sort)
- 堆排序(heap sort)
- 堆排序(Heap Sort)
- 堆排序(Heap-Sort)
- Heap Sort(堆排序)
- 堆排序(Heap Sort)
- 堆排序(Heap Sort)
- 堆排序(Heap Sort)
- 堆排序 (Heap Sort)
- 堆排序(Heap Sort)
- heap sort 堆排序
- 企业云化的几个阶段
- 买卖股票的最佳时机 III-LintCode
- 圆的反演模版
- 本渣的Python学习之路——1(replay)
- (二)蓝牙低功耗(BLE)基础教程--基于nRF5x系列SOC
- 堆排序(heap sort)
- 问题 : 数组中和等于K的数对
- 算法与数据结构
- 拉普拉斯矩阵/映射/聚类
- Linux系统启动过程
- python的yield使用
- 一:ubuntu14.04下配置OpenCV3.3.0和Python2.7
- CodeForces
- 转载:git概念 原理 使用