In C# microsoft visual studios in the console app with a black box output. Imple
ID: 3717390 • Letter: I
Question
In C# microsoft visual studios in the console app with a black box output.
Implementing an efficient sort algorithm (Heapsort). In this assignment you are asked to create a MaxHeap class capable of holding integer values. The class must expose three public methods: 1 public void Add( int newltem) 2 public int RemoveO 3 public void Show Respectively, the methods are responsible for (1) insert a new element in the heap, (2) remove the root of the heap, and )nicely show the contents of the heap Test your application with the following sample: int® list-2, 9, 5, 4, S. ?, 7 };Explanation / Answer
Answer:
here, I find the code for C# using console application
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication16
{
// implement a class
class myHeapSort
{
// declare array varaible
// as given in the question
int[] list = { 2, 9, 5, 4, 8, 1, 6, 7 };
// define a Remove() method
public void Remove()
{
// declare a varaible
int tp;
for (int x = 5; x >= 0; x--)
{
Add(x, 9);
}
for (int m = 8; m >= 0; m--)
{
tp = list[m + 1];
list[m + 1] = list[0];
list[0] = tp;
Add(0, m);
}
}
// define a Add() method
private void Add(int a, int number)
{
int tp, i;
try
{
tp = list[a];
i = 2 * a;
// use loop
while (i <= number)
{
// check
if (i < number && list[i] < list[i + 1])
i++;
if (tp >= list[i])
break;
list[i / 2] = list[i];
i *= 2;
}
list[i / 2] = tp;
}
// else error throw
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Array Out of Bounds ", e);
}
}
// define a Show() method
public void show()
{
// use loop
for (int x = 0; x <= 10; x++)
{
Console.WriteLine("{0}", list[x]);
}
}
// main function
static void Main(string[] args)
{
myHeapSort Heap = new myHeapSort();
Console.WriteLine("The values before the soriting: ");
Heap.show();
Heap.Remove();
Console.WriteLine("Values After sorting : ");
Heap.show();
Console.Read();
}
}
}