DAA
DAA
In a max-heap, the maximum element is always at the root. Deleting the maximum element involves removing the root node and then reorganizing the heap to maintain the max-heap property.
Here are the steps to delete the maximum element from a max-heap:
To delete the maximum element, we replace the root (maximum element) with the last element of the heap.
After replacing the root, we remove the last element from the heap. This ensures that the heap remains complete.
After replacing the root, the max-heap property might be violated. We need to restore this property by performing a heapify operation.
Heapify Operation:
Starting from the root, we compare the parent node with its children. We swap the parent node with the larger child (if the parent is smaller than its children) until the max-heap property is restored.
code:
#include <stdio.h>
*a = *b;
*b = temp;
int largest = i;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
return -1;
(*n)--;
1
return arr[0];
(*n)--;
return maxElement;
printf("\n");
int main() {
printArray(arr, n);
if (deletedElement != -1)
else
printArray(arr, n);
return 0;
Binary search is a fundamental algorithm used to search for a specific element in a sorted array efficiently.
Binary search is an efficient algorithm used to find the position of a target value within a sorted array. It works by repeatedly dividing in half the portion of the array that could contain the target value, and then
comparing the target value with the middle element of the resulting subarray.
Compare: Compare the target value with the middle element of the array.
Divide: If the target value matches the middle element, return its index. If the target value is less than the middle element, continue the search on the left half of the array. If the target value is greater,
continue the search on the right half.
Repeat: Repeat the process until the target value is found or the subarray is empty.
Terminate: If the target value is not found after the entire array is searched, return a "not found" indication.
The time complexity of binary search is O(log n), where n is the number of elements in the array. This is because, with each comparison, the size of the search space is halved. Therefore, binary search is highly
2
efficient for large sorted arrays.