heap

increase_up · updated November 14, 2024
#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

Comments

Please sign up or log in to contribute to the discussion.