-
Notifications
You must be signed in to change notification settings - Fork 1
/
heapsort.c
54 lines (50 loc) · 835 Bytes
/
heapsort.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
#include <stdio.h>
#define max 5
int a[max];
void heapsort( int n);
void heapify( int i,int n);
void main()
{
printf("Enter the elements of the array : ");
for(int i=0;i<max;i++)
scanf("%d",&a[i]);
heapsort(max);
printf("The sorted array: ");
for(int i=0;i<max;i++)
printf("%d ",a[i]);
}
void heapsort(int n)
{
for(int i= n/2-1 ;i>=0;i--)
{
heapify(i,n);
}
for(int i=n-1 ;i>=0;i--)
{
int temp=a[0];
a[0]=a[i];
a[i]=temp;
heapify(0,i);
}
}
void heapify( int i,int n)
{ int largest,lc,rc,temp;
largest=i;
lc=2*i+1 ;
rc=2*i + 2;
if((a[lc]>a[largest])&&(lc<n))
{
largest=lc;
}
if((a[rc]>a[largest])&&(rc<n))
{
largest=rc;
}
if(i!=largest)
{
temp=a[i];
a[i]=a[largest];
a[largest]=temp;
heapify(largest,n);
}
}