0% found this document useful (0 votes)
8 views5 pages

Heap Sort

Heap Sort is a comparison-based sorting algorithm that builds a binary max-heap from input data and sorts it by repeatedly swapping the root with the last element and heapifying. The process involves constructing the max-heap, swapping elements, reducing the heap size, and restoring the heap property until the heap size is one. The document also includes pseudocode and C code implementations for the Heap Sort algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Heap Sort

Heap Sort is a comparison-based sorting algorithm that builds a binary max-heap from input data and sorts it by repeatedly swapping the root with the last element and heapifying. The process involves constructing the max-heap, swapping elements, reducing the heap size, and restoring the heap property until the heap size is one. The document also includes pseudocode and C code implementations for the Heap Sort algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Heap Sort

1. Heap Sort Algorithm:


Heap Sort is a comparison-based sorting algorithm. It works by building a binary heap
(either max-heap or min-heap) from the input data and then sorting it.
Steps:
1. Build a Max Heap:
o Construct a binary heap from the input data. In a Max Heap, the value of
each parent node is greater than or equal to the values of its children.
2. Swap the Root with the Last Element:
o The root of the Max Heap is the maximum element. Swap the root with the
last element of the heap (array).
3. Reduce the Heap Size:
o After swapping, the heap size is reduced by 1 because the last element is
now in its correct sorted position.
4. Heapify the Root:
o Restore the Max Heap property by calling the heapify function on the root
of the heap (which was affected after the swap).
5. Repeat the process until the heap size becomes 1.

2. Pseudocode for Heap Sort:


HeapSort(A):
n = length of A
BuildMaxHeap(A, n)

for i = n - 1 down to 1 do:


swap A[0] with A[i]
n=n-1
Heapify(A, 0, n)

BuildMaxHeap(A, n):
for i = floor(n/2) - 1 down to 0 do:
Heapify(A, i, n)

Heapify(A, i, n):
left = 2 * i + 1
right = 2 * i + 2
largest = i
if left < n and A[left] > A[largest]:
largest = left

if right < n and A[right] > A[largest]:


largest = right

if largest != i:
swap A[i] with A[largest]
Heapify(A, largest, n)

3. C Code for Heap Sort:


#include <stdio.h>
// Function to swap two elements
void swap (int* a, int* b)
{
int temp = *a; *a = *b; *b = temp;
}
// Function to print an array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
// Function to heapify a subtree rooted with node i which is
an index in arr []

void heapify (int arr [], int n, int i)


{
// Initialize largest as root
int largest = i;
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

if (largest!= i)
{ swap(&arr[i], &arr[largest]);
heapify (arr, n, largest);
}
}
void buildMaxHeap(int arr[], int n)
{
// Start from the last non-leaf node and heapify each
node
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}
//Function to implement heap sort

void heapSort(int arr[], int n)


{
// Build a max heap (rearrange the array)
buildMaxHeap(arr, n);

// One by one extract elements from the heap

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


{
// Move the current root to the end
swap(&arr[0], &arr[i]);

// Call heapify on the reduced heap


heapify (arr, i, 0);
}
}
int main ()
{ int n;
printf("Enter the size of array: ");
scanf("%d",&n);
int arr[n];
printf("Enter elements for array:\n");
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Unsorted array: \n");
printArray(arr, n);
heapSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy