-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.c
More file actions
49 lines (41 loc) · 896 Bytes
/
Copy pathMaxHeap.c
File metadata and controls
49 lines (41 loc) · 896 Bytes
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
26 } 26 } 26 }#include<stdio.h>
#include<stdlib.h>
void maxheapify(int *arr,int heapsize,int i)
{
int left = 2*i+1;
int right = 2*i+2;
int largest;
if(left < heapsize && arr[left]>arr[i])
largest = left;
else
largest = i;
if(right < heapsize && arr[right]>arr[largest])
largest = right;
if(largest != i)
{
int temp;
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
maxheapify(arr,heapsize,largest);
}
}
void Heapsort(int *arr,int size)
{
int HeapSize = size;
int HeapLength = HeapSize;
// Find largest non-leaf index.
for(int i=((HeapLength/2)-1);i>=0;i--)
maxheapify(arr,HeapSize,i);
}
int main()
{
int str[] = {4,45,23,2,6,7,33,32,34,55};
int len,i;
len = sizeof(str)/sizeof(str[0]);
Heapsort(str,len);
for (i=0;i<len;i++)
printf("%d ",str[i]);
printf("\n");
return 0;
}