-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
156 lines (146 loc) · 2.74 KB
/
heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "heap.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void initHeap(Heap *h,int sD,int sH)
{
h->sizeData=sD;
h->elems=0;
h->maxelems=sH;
h->data=malloc(sD*sH);
}
int emptyH(Heap *h)
{
if(h->elems==0)
return 1;
return 0;
}
int full(Heap *h)
{
if(h->elems>=h->maxelems)
return 1;
return 0;
}
void shiftup(Heap *h,int compare(void *,void *))
{
int root,flag;
void *temp,*n1,*n2;
root=h->elems;
temp=malloc(h->sizeData);
while(root>1)
{
n1=h->data+((root-1)*h->sizeData);
root=root/2;
n2=h->data+((root-1)*h->sizeData);
flag=compare(n1,n2);
if(flag<0)
{
memcpy(temp,n1,h->sizeData);
memcpy(n1,n2,h->sizeData);
memcpy(n2,temp,h->sizeData);
}
else
break;
}
free(temp);
return;
}
void shiftdown(Heap *h,int compare(void *,void *))
{
void *n1,*n2,*n3,*t;
int root=1,child,swap;
t=malloc(h->sizeData);
while((2*root)<=h->elems)
{
child=(2*root);
swap=root;
n1=h->data+((swap-1)*h->sizeData);
n2=h->data+((child-1)*h->sizeData);
n3=h->data+((child)*h->sizeData);
if(compare(n1,n2)>0)
swap=child;
if(child+1<=h->elems && compare(n1,n3)>0)
{
if(compare(n2,n3)<0)
swap=child;
else
swap=child+1;
}
if(swap!=root)
{
memcpy(t,h->data+((swap-1)*h->sizeData),h->sizeData);
memcpy(h->data+((swap-1)*h->sizeData),h->data+((root-1)*h->sizeData),h->sizeData);
memcpy(h->data+((root-1)*h->sizeData),t,h->sizeData);
root=swap;
}
else
break;
}
free(t);
}
void insert2Heap(Heap *h,void *el,int size,int compare(void *,void *))
{
if(size!=h->sizeData || full(h))
{
printf("Size of element not equal to element size of heap\n");
exit(1);
}
void *temp=h->data+(h->sizeData*h->elems);
memcpy(temp,el,h->sizeData);
h->elems++;
shiftup(h,compare);
}
void *del4mHeap(Heap *h,int compare(void *,void *))
{
void *temp=malloc(h->sizeData);
if(h->elems<=0)
{
printf("Deletion from heap not possible\n");
exit(1);
}
memcpy(temp,h->data,h->sizeData);
memcpy(h->data,h->data+((h->elems-1)*h->sizeData),h->sizeData);
memcpy(h->data+((h->elems-1)*h->sizeData),temp,h->sizeData);
h->elems--;
shiftdown(h,compare);
return temp;
}
void heapsort(void *arr,int elsize,int size,int compare(void *,void *))
{
Heap h;
void *element;
initHeap(&h,elsize,size);
int i;
for(i=0;i<size;i++)
{
insert2Heap(&h,arr+i*elsize,elsize,compare);
}
i=0;
while(!emptyH(&h))
{
element=del4mHeap(&h,compare);
memcpy((arr+i*elsize),element,elsize);
i++;
}
}
/*int compareInts(void *a,void *b)
{
int *n1,*n2;
n1=(int *)a;
n2=(int *)b;
if((*n1)>(*n2))
return 1;
else if((*n1)<(*n2))
return -1;
return 0;
}
main()
{
Heap H;
int n,*t,i;
int arr[]={5,4,3,7,100};
heapsort(arr,sizeof(int),5,compareInts);
for(i=0;i<5;i++)
printf("%d ",arr[i]);
printf("\n");
}*/