Write a c program to build a heap method using Pointer to
function and pointer to structure ?
Answer / Balraj Singh
Here is a simple example of a C program that uses pointers to functions and structures to implement a heap sort algorithm:n```n#include <stdio.h>nn// Define the structure for an element in the heapnstruct HeapElement {n int data;n int index;n};nn// Define the function pointer type for the swap functionntypedef void SwapFunc(struct HeapElement*, struct HeapElement*);nn// Define the swap functionnvoid Swap(struct HeapElement* a, struct HeapElement* b) {n struct HeapElement temp = *a;n *a = *b;n *b = temp;n}nn// Implement the heapify function that maintains the heap property for a given subtree rooted at the index idxnvoid Heapify(struct HeapElement heap[], int idx, SwapFunc swap, int size) {n int largest = idx;n int left = 2 * idx + 1;n int right = 2 * idx + 2;nn // If the left child is larger than rootn if (left < size && heap[left].data > heap[largest].data) {n largest = left;n }nn // If the right child is larger than root and largest is not the left childn if (right < size && heap[right].data > heap[largest].data && largest != left) {n largest = right;n }nn // If the largest index is not the root indexn if (largest != idx) {n Swap(&heap[idx], &heap[largest]);n Heapify(heap, largest, swap, size);n }n}nn// Implement the buildHeap function that builds a heap from an unsorted array of elementsnvoid BuildHeap(struct HeapElement heap[], int size, SwapFunc swap) {n for (int i = size / 2 - 1; i >= 0; i--) {n Heapify(heap, i, swap, size);n }n}nn// Implement the heapSort function that sorts an array of elements using heap sort algorithmnvoid HeapSort(struct HeapElement heap[], int size, SwapFunc swap) {n BuildHeap(heap, size, swap);nn for (int i = size - 1; i > 0; i--) {n Swap(&heap[0], &heap[i]);n Heapify(heap, 0, swap, i);n }n}nn// Main function that takes an array of integers as input and sorts it using heap sort algorithmnint main() {n int arr[] = {12, 11, 13, 5, 6, 7};n int n = sizeof(arr) / sizeof(arr[0]);nn struct HeapElement heap[n];nn for (int i = 0; i < n; i++) {n heap[i].data = arr[i];n heap[i].index = i;n }nn HeapSort(heap, n, &Swap);nn for (int i = 0; i < n; i++) {n printf("%d ", heap[i].data);n }n return 0;n}n```
| Is This Answer Correct ? | 0 Yes | 0 No |
What are the types of pointers in c?
what is the output of the following code? main() { int I; I=0x10+010+10; printf("x=%x",I); } give detailed reason
increment operateor (++)and decrament(--) #include<stdio.h> #inclide<conio.h> main() { int x=15; while(x!=0) scanf("%d",&x); {
in case any function return float value we must declare a) the function must be declared as 'float' in main() as well b) the function automatically returned float values c) function before declared 'float' keyword d) all the above
Do you know the difference between malloc() and calloc() function?
There are N egg baskets and the number of eggs in each basket is a known quantity. Two players take turns to remove these eggs from the baskets. On each turn, a player must remove at least one egg, and may remove any number of eggs provided they all belong to the same basket. The player picking the last egg(s) wins the game. If you are allowed to decide who is going to start first, what mathematical function would you use to decide so that you end up on the winning side?
In which language linux is written?
What is calloc()?
What is nested structure in c?
can we declare a function inside the structure? ex: struct book { int pages; float price; int library(int,float); }b; is the above declaration correct? as it has function declaration?
main() { int i,j,A; for(A=-1;A<=1;A++) prinf("%d\t",!!A); }
Difference between macros and inline functions? Can a function be forced as inline?
1 Answers HAL, Honeywell, Zomato,