-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_sort.c
73 lines (73 loc) · 1002 Bytes
/
heap_sort.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
#include <stdio.h>
int n=10;
//int heap_size=9;
int parent(int i)
{
return (i-1)/2;
}
int left(int i)
{
return 2*i+1;
}
int right(int i)
{
return 2*i+2;
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void max_heapify(int *arr,int i)
{
int l=left(i);
int r=right(i);
int largest;
if(l<n && arr[l]>arr[i])
largest=l;
else
largest=i;
if(r<n && arr[r]>arr[largest])
largest=r;
if(largest!=i)
{
swap(&arr[largest],&arr[i]);
max_heapify(arr,largest);
}
}
void build_max_heap(int *arr)
{
for(int i=(n/2)-1;i>=0;i--)
max_heapify(arr,i);
}
void heap_sort(int *arr)
{
build_max_heap(arr);
for(int i=n-1;i>0;i--)
{
swap(&arr[0],&arr[i]);
n=n-1;
max_heapify(arr,0);
}
}
void print(int *arr)
{
for(int i=0;i<10;i++)
printf("%i ",arr[i]);
printf("\n");
}
int main()
{
//int n=10;
int arr[]={4,1,3,2,16,9,10,14,8,7};
/*int l,r,p;
l=left(0);
r=right(0);
p=parent(10);
printf("%i\n%i\n%i\n",l,r,p);*/
print(arr);
heap_sort(arr);
print(arr);
return 0;
}