-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart 2- 1.c
More file actions
56 lines (45 loc) · 1015 Bytes
/
part 2- 1.c
File metadata and controls
56 lines (45 loc) · 1015 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
56
#include<stdio.h>
void swap(int *a, int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
void MinHeapify(int A[],int n,int i){
int smallest=i;
int l=2*i+1;
int r=2*i+2;
while(l<n && A[l]<A[smallest])smallest=l;
while(r<n && A[r]<A[smallest])smallest=r;
if(smallest!=i){
swap(&A[smallest],&A[i]);
MinHeapify(A,n,smallest);
}
}
void BuildMinHeap(int A[],int n){
for(int i=n/2-1;i>=0;i--) MinHeapify(A,n,i);
for (int i =n-1;i>0;i--) {
swap(&A[0], &A[i]);
MinHeapify(A,i, 0);
}
}
void HeapInsertion(int A[],int n,int value){
A[n-1]=value;
BuildMinHeap(A,n);
}
int main(){
int n=0,i,A[100],x;
for(i=0;;i++){
printf("Enter numbers: ");
scanf("%d",&x);
if(x==EOF) break;
n++;
HeapInsertion(A,n,x);
}
printf("The min heap:\n");
for(i=0;i<n;i++){
printf("%8d ",A[i]);
}
printf("\n");
return 0;
}