# Heap Sort in C#

CsharpProgrammingServer Side Programming

Heap Sort is a sorting algorithm that makes use of the heap data structure. Each time the root element of the heap i.e. the largest element is removed and stored in an array. It is replaced by the rightmost leaf element and then the heap is reestablished. This is done until there are no more elements left in the heap and the array is sorted.

A program that demonstrates heap sort in C# is given as follows.

## Example

Live Demo

using System;
namespace HeapSortDemo {
public class example {
static void heapSort(int[] arr, int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n-1; i>=0; i--) {
int temp = arr;
arr = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void Main() {
int[] arr = {55, 25, 89, 34, 12, 19, 78, 95, 1, 100};
int n = 10, i;
Console.WriteLine("Heap Sort");
Console.Write("Initial array is: ");
for (i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
heapSort(arr, 10);
Console.Write("\nSorted Array is: ");
for (i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
}

## Output

The output of the above program is as follows.

Heap Sort
Initial array is: 55 25 89 34 12 19 78 95 1 100
Sorted Array is: 1 12 19 25 34 55 78 89 95 100

Now, let us understand the above program.

The function main() contains the array arr. It prints the initial array and then calls the function heapSort() that will sort the array. This can be seen in the following code snippet.

int[] arr = {55, 25, 89, 34, 12, 19, 78, 95, 1, 100};
int n = 10, i;
Console.WriteLine("Heap Sort");
Console.Write("Initial array is: ");
for (i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
heapSort(arr, 10);

The function heapSort() first converts the given elements into a heap. This is done by using the for loop and calling the function heapify() for all the non-leaf elements of the heap. This can be seen in the following code snippet.

for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

After the heap is created, a for loop is used to remove the root element of the heap i.e. the largest element. It is replaced by the rightmost leaf element and then heapify() is called again to reestablish the heap. This can be seen in the following code snippet.

for (int i = n-1; i>=0; i--) {
int temp = arr;
arr = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}

The function heapify() creates a heap structure by arranging the elements as required. This process starts from the element at index i for this is considered the root element for the heapify() function. This can be seen in the following code snippet.

int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}

Finally, the sorted array is displayed in the main() function. This can be seen in the following code snippet.

Console.Write("\nSorted Array is: ");
for (i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}