0% found this document useful (0 votes)
22 views

Data Structures

The document contains code for implementing a max heap data structure and related functions like building a max heap from an array, inserting and deleting elements, and using it to implement heapsort. It includes functions for swapping elements, reheaping up and down the tree, building the heap from an array, inserting and deleting elements, and using it to implement heapsort on an example array.

Uploaded by

snehith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Data Structures

The document contains code for implementing a max heap data structure and related functions like building a max heap from an array, inserting and deleting elements, and using it to implement heapsort. It includes functions for swapping elements, reheaping up and down the tree, building the heap from an array, inserting and deleting elements, and using it to implement heapsort on an example array.

Uploaded by

snehith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>

#include <stdlib.h>

#define MAX_HEAP_SIZE 100

typedef struct {

int heap[MAX_HEAP_SIZE];

int size;

} MaxHeap;

// Function to swap two elements

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

// Reheap up

void reheap_up(MaxHeap *heap, int index) {

int parent = (index - 1) / 2;

while (index > 0 && heap->heap[index] > heap->heap[parent]) {

swap(&heap->heap[index], &heap->heap[parent]);

index = parent;

parent = (index - 1) / 2;

// Reheap down
void reheap_down(MaxHeap *heap, int index) {

int left_child, right_child, larger_child;

while ((left_child = 2 * index + 1) < heap->size) {

right_child = left_child + 1;

larger_child = left_child;

if (right_child < heap->size && heap->heap[right_child] > heap->heap[left_child]) {

larger_child = right_child;

if (heap->heap[index] >= heap->heap[larger_child]) {

break;

swap(&heap->heap[index], &heap->heap[larger_child]);

index = larger_child;

// Build max heap

void build_max_heap(MaxHeap *heap, int arr[], int n) {

heap->size = n;

for (int i = 0; i < n; i++) {

heap->heap[i] = arr[i];

for (int i = (n - 1) / 2; i >= 0; i--) {

reheap_down(heap, i);

// Insert into max heap


void insert(MaxHeap *heap, int value) {

if (heap->size >= MAX_HEAP_SIZE) {

printf("Heap overflow!\n");

return;

heap->heap[heap->size++] = value;

reheap_up(heap, heap->size - 1);

// Delete max element from max heap

int delete_max(MaxHeap *heap) {

if (heap->size == 0) {

printf("Heap underflow!\n");

return -1; // Return some sentinel value

int max_value = heap->heap[0];

heap->heap[0] = heap->heap[--heap->size];

reheap_down(heap, 0);

return max_value;

// Heapsort

void heapsort(int arr[], int n) {

MaxHeap heap;

build_max_heap(&heap, arr, n);

for (int i = n - 1; i > 0; i--) {

swap(&heap.heap[0], &heap.heap[i]);

heap.size--;
reheap_down(&heap, 0);

// Utility function to print array

void print_array(int arr[], int n) {

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

print_array(arr, n);

// Heapsort

heapsort(arr, n);

printf("Sorted array using heapsort: ");

print_array(arr, n);

return 0;
}

You might also like