-
Notifications
You must be signed in to change notification settings - Fork 0
/
week8-6.c
74 lines (67 loc) · 1.18 KB
/
week8-6.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
int length=0;
void printHeap(int heap[]){
int i;
for(i=1;i<=length;i++){
printf("%d ",heap[i]);
}
}
void max_heapify(int arr[],int i){
int left=2*i;
int right=2*i+1;
int largest=i;
if(left<=length&&arr[left]>arr[i]){
largest=left;
}
if(right<=length&&arr[right]>arr[largest]){
largest=right;
}
if(largest!=i){
int temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
max_heapify(arr,largest);
}
return ;
}
int extractMax(int heap[]){
int temp=heap[1];
//printf("swapped with %d\n",heap[length]);
heap[1]=heap[length--];
printf("%d\n",heap[1]);
max_heapify(heap,1);
return temp;
}
void Klargest(int heap[],int k){
int temp[length+1],i;
for(i=1;i<=length;i++){
temp[i]=heap[i];
}
int kth;
for(i=0;i<k;i++){
kth=extractMax(temp);
}
printf("%d\n",kth);
}
void Insert(int heap[],int value){
length=length+1;
heap[length]=value;
int i=length;
while(i>1&&heap[i]>heap[i/2]){
int temp=heap[i];
heap[i]=heap[i/2];
heap[i/2]=temp;
i=i/2;
}
}
int main(){
int arr[1000];
Insert(arr,56);
Insert(arr,299);
Insert(arr,35);
Insert(arr,45);
printf("%d\n",extractMax(arr));
printHeap(arr);
Klargest(arr,3);
}