C# Program to Implement Heap Sort

This is a C# Program to demonstrate heap sort.

Problem Description

This C# Program Demonstrates Heap Sort.

Problem Solution

Here it first removes the topmost item (the largest) and replace it with the rightmost leaf. The topmost item is stored in an array and Re-establish the heap.this is done until there are no more items left in the heap.

Program/Source Code

Here is source code of the C# Program to Demonstrate Heap Sort. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate Heap Sort
 */
using System;
class heap
{
    int[] r = { 2,5,1,10,6,9,3,7,4,8};
    public void hsort()
    {
        int i, t;
        for (i = 5; i >= 0; i--)
        {
            adjust(i, 9);
        }
        for (i = 8; i >= 0; i--)
        {
            t = r[i + 1];
            r[i + 1] = r[0];
            r[0] = t;
            adjust(0, i);
        }
    }
    private void adjust(int i, int n)
    {
        int t, j;
        try
        {
            t = r[i];
            j = 2 * i;
            while (j <= n)
            {
                if (j < n && r[j] < r[j + 1])
                    j++;
                if (t >=r[j])
                    break;
                r[j / 2] = r[j];
                j *= 2;
            }
            r[j / 2] = t;
        }
        catch (IndexOutOfRangeException e)
        {
            Console.WriteLine("Array Out of Bounds ", e);
        }
    }
    public void print()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("{0}", r[i]);
        }
 
    }
    public static void Main()
    {
        heap obj = new heap();
        Console.WriteLine("Elements Before sorting : ");
        obj.print();
        obj.hsort();
        Console.WriteLine("Elements After sorting : ");
        obj.print();
        Console.Read();
    }
}
Program Explanation

This C# program is used to demonstrate heap sort. Hence we have defined the r[] array variable values. Using for loop we are checking the value of ‘i’ variable is greater than the value of array index variable to index 5. If the condition is true then execute the statement.

advertisement

In another for loop check that variable value is greater than the value of array index variable upto index 8. The adjust() method is used to first remove the topmost item and replace it with the rightmost leaf.

The topmost item is stored in an array and re-establishes the heap. This is done until there are no more items left in the heap. By using the exception handling check the array index is out of range then executes the catch statement and print the statement as array out of bounds.

Runtime Test Cases
 
Elements Before Sorting :
2
5
1
10
6
9
3
7
4
8
Elements After Sorting :
1
2
3
4
5
6
7
8
9
10

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.