HeapSort(Heap *heap, size_t array[], size_t size );
功能:传入一个数组给这个数组排序
思路:把数组中的元素一个一个的插入到堆中,然后在依次擦除,为甚么能这么做?因为我们对堆顶元素删除时,
是把堆顶元素和堆尾元素对调位置,size–,如果是大堆,最大值就跑到数组最后边,为升序排列,如果
是小堆,最小值就跑到数组最后边,为降序排列
1.根据数组size大小,循环
Heapinsert(heap,array[i]);
2.根据数组size大小,循环
Heaperase( heap); //每擦除一个heap->size减一,不能用print打印结果,
因为循环完size为0无法打印,所以赋值给数组
3.给数组赋值
memcpy(array,heap->data,size*sizeof(类型));
(小堆)堆的插入:判断 参数合法以及堆空间
数组元素一个一个传入,传一个进行一次adjustup(heap,heap->size-1)heap->size-1表示堆尾新插入 的元素的下标
adjustup()就是每插一个元素,判断插入的元素与它的父节点的值的大小,如果小于父节点就交换,在循环, 否则跳出循环
如果我插入时堆为空,没有父节点那就不进入循环while(child>0)
堆的删除:判断 参数合法以及堆是否为空
把堆顶元素和堆尾元素对调位置,size– adjustdown(heap);
adjustdown()就是根节点现在是最大值,找到child和child+1中较小的赋值给child,这里child+1要小 于heap->size
把child和parent比较,如果child小于parent就交换值,循环,否则跳出
while中child>0&&childsize && parent>=0&&parentsize
1 #pragma once
2 #include <stdio.h>
3 #include<string.h>
4
5 #define MAX_SIZE 100
6
7 typedef char HeapType;
8 typedef int (*compare)(HeapType a, HeapType b);
9 typedef struct Heap
10 {
11 HeapType data[MAX_SIZE];
12 int size;
13 compare cmp;
14 }Heap;
15
16 void Heapinit(Heap *heap,compare cmp);
17 void Heapdesdroy(Heap *heap);
18 void Heapinsert(Heap *heap,HeapType value);
19 void Heaperase(Heap *heap);
20 void Heapsort(Heap *heap,HeapType array[],size_t size);
#include "heap.h"
2 int Less(HeapType a,HeapType b)
3 {
4 return a<b ? 1:0;
5 }
6 int Greater(HeapType a,HeapType b)
7 {
8 return a>b ? 1:0;
9 }
10 void Heapinit(Heap *heap,compare cmp)
11 {
12 if(heap==NULL)
13 return ;
14 heap->size=0;
15 heap->cmp=cmp;
16 }
17 void Heapdesdroy(Heap *heap)
18 {
19 if(heap==NULL)
20 return ;
21 heap->size=0;
22 }
23 void swap(HeapType *a,HeapType *b)
24 {
25 HeapType temp;
26 temp=*a;
27 *a=*b;
28 *b=temp;
29 }
30 void adjustup(Heap *heap,size_t index)
31 {
32 int child=index;
33 int parent=(child-1)/2;
34 while(child>0)
35 {
36 if(heap->cmp(heap->data[child] , heap->data[parent])) //函数指针,如果孩子结点小于父节点,小堆
37 swap(&heap->data[child],&heap->data[parent]);
38 else
39 break;
40 child=parent;
41 parent=(child-1)/2;
42 }
43 }
44 void Heapinsert(Heap *heap,HeapType value)
45 {
46 if(heap==NULL)
47 return ;
48 if(heap->size>MAX_SIZE)
49 return;
50 heap->data[heap->size++]=value;
51 adjustup(heap,heap->size-1);//!
52 }
53 void adjustdown(Heap *heap)
54 {
55 int parent = 0;
56 int child = (parent*2)+1;
57 while(child>0 && child<heap->size && parent>=0 && parent<heap->size)
58 {
59 if(child+1<heap->size&&heap->data[child+1]<heap->data[child])
60 child=child+1;
61 if(!heap->cmp(heap->data[parent],heap->data[child])) //用函数指针
62 swap(&heap->data[parent],&heap->data[child]);
63 else
64 break;
65 parent=child;
66 child=(parent*2)+1;
67 }
68 }
69 void Heaperase(Heap *heap)
70 {
71 if(heap==NULL)
72 return;
73 if(heap->size==0)
74 return;
75 swap(&heap->data[0],&heap->data[heap->size-1]);
76 heap->size--;
77 adjustdown(heap);
78
79 }
80 void Heapsort(Heap *heap,HeapType array[],size_t size)
81 {
82 int i=0,j=0;
83 for(;i<size;i++)
84 {
85 Heapinsert(heap,array[i]);
86 }
87 //while(heap->size>0)
88 for(j;j<size;j++)
89 {
90 Heaperase(heap);//每擦除一个,size就--,所以不能用print函数打印结果
91 }
92 memcpy(array,heap->data,size*sizeof(HeapType));
93 }
94 void print(Heap *heap)
95 {
96 if(heap==NULL)
97 return;
98 int i=0;
99 for(i;i<heap->size;i++)
100 {
101 printf("%c ",heap->data[i]);
102 }
103 }
104 int main()
105 {
106 Heap heap;
107 Heapinit(&heap,Less);
108 // Heapinsert(&heap,'9'); // 9 9 5 2 2 1
109 // Heapinsert(&heap,'5'); //5 2 9 9 5 7 5 2 5
110 // Heapinsert(&heap,'2'); //7 1 9 9 7
111 // Heapinsert(&heap,'7');
112 // Heapinsert(&heap,'1');
113 // Heaperase(&heap);
114 char a[]="2145687943";
115 Heapsort(&heap,a,10);
116 int i=0;
117 for(;i<10;i++)
118 printf("%c ",a[i]);
119 // print(&heap);
120 return 0;
121 }