-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e64937
commit fbffb1d
Showing
3 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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 | ||
*/ | ||
|
||
#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); | ||
} | ||
|
||
display(arr,size); | ||
} | ||
|
||
int main(){ | ||
int arr[] = {9,6,5,0,8,2,1,3}; | ||
int size = sizeof(arr)/sizeof(arr[0]); | ||
|
||
makeHeap(arr,size); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters