-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.c
More file actions
57 lines (57 loc) · 1.04 KB
/
heapsort.c
File metadata and controls
57 lines (57 loc) · 1.04 KB
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
#include<stdio.h>
#include<stdlib.h>
int heap_size;
void heap_sort(int *arr);
void max_heapify(int *arr,int i);
void build_max_heap(int *arr);
int main()
{
int n,*arr,i;
printf("Enter the size of an array : ");
scanf("%d",&n);
arr=(int*)calloc(n,sizeof(int));
printf("Enter array elements : ");
for(i=0;i<n;i++)
scanf("%d",(arr+i));
heap_size=n-1;
heap_sort(arr);
printf("Array elements are : ");
for(i=0;i<n;i++)
printf("%d ",*(arr+i));
}
void build_max_heap(int *arr)
{
int i;
for(i=(heap_size)/2;i>=0;i--)
max_heapify(arr,i);
}
void max_heapify(int *arr,int i)
{
int l=2*i+1,r=2*i+2,largest,temp;
if(l<=heap_size&&arr[l]>arr[i])
largest=l;
else largest=i;
if(r<=heap_size&&arr[r]>arr[largest])
largest=r;
if(largest!=i)
{
temp=arr[largest];
arr[largest]=arr[i];
arr[i]=temp;
max_heapify(arr,largest);
}
}
void heap_sort(int *arr)
{
int temp,i,n;
n=heap_size;
build_max_heap(arr);
for(i=n;i>=1;i--)
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heap_size--;
max_heapify(arr,0);
}
}