heap
C
#include <stdio.h>
//heapSort(int arr[]);
insert(int arr[]);
arrPrint(int arr[]);
int main() {
int arr[11] = {-1,};
insert(arr);
arrPrint(arr);
}
void insert(int arr[]) {
int root, temp, leaf;
for (int i=1;i<=10;i++) {
leaf = i;
scanf("%d",&arr[i]); // enter data
while (leaf != 1) {
root = leaf/2; // parents node data index
if (arr[root] < arr[leaf]) { // consist of two data;
temp = arr[root];
arr[root] = arr[leaf];
arr[leaf] = temp;
leaf = root; // index change
}
else break; // root > leaf = maxheap
}
}
}
void arrPrint(int arr[]) {
int cnt = 1;
while (arr[cnt] != 0) { printf("%d",arr[cnt]); cnt++;}
}
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.