-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion1.c
65 lines (55 loc) · 1.39 KB
/
question1.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
/*
Given an array, create a max heap
METHOD1:
Sorting in descending order for max heap and ascending for min heap
Time complexity: O(nlogn)
Space complexity: O(1)
METHOD2:
Using max heapify algorithm
Time complexity: O(n) because the other component logn is merely constant because at each level the height
increases which is in the denominator and also the number of nodes decreases, therefore it is better than
sorting.
Space complexity: O(logn) //because of recursion this is the max stack size for execution required which
is equal to the height of the tree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void maxHeapify(int arr[],int i, int size){
int left = 2*i+1, right = 2*i+2;
int heapSize = size, largest, temp;
if(left <= heapSize-1 && arr[i] > arr[left]){
largest = i;
}else{
largest = left;
}
if(right <= heapSize-1){
if(arr[largest] < arr[right]){
largest = right;
}
}
if(largest <= heapSize && largest != i){
temp = arr[largest];
arr[largest] = arr[i];
arr[i] = temp;
maxHeapify(arr,largest,size);
}
}
void display(int arr[],int size){
for(int i=0; i<size;i++){
printf("%d\t", arr[i]);
}
}
void makeHeap(int arr[],int size){
int start = floor(size/2)-1;
for(int i=start;i>=0;i--){
maxHeapify(arr,i,size);
}
}
int main(){
int arr[] = {9,6,5,0,8,2,1,3};
int size = sizeof(arr)/sizeof(arr[0]);
makeHeap(arr,size);
display(arr,size);
return 0;
}