-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.c
More file actions
55 lines (55 loc) · 936 Bytes
/
heap.c
File metadata and controls
55 lines (55 loc) · 936 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
50
51
52
53
54
55
#include<stdio.h>
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void heapify(int a[],int n,int i)
{
int large=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && a[left]>a[large])
{
large=left;
}
if(right<n && a[right]>a[large])
{
large=right;
}
if(large!=i)
{
swap(a,i,large);
heapify(a,n,large);
}
}
void sort(int a[],int n)
{
int i;
for(i= n/2-1;i>=0;i--)
heapify(a,n,i);
for(i=n-1;i>=0;i--)
{
swap(a,0,i);
heapify(a,i,0);
}
}
void main()
{
int i,n,a[25];
printf("enter limit");
scanf("%d",&n);
printf("enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,n);
printf("sorted array is: \n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}