C# – Heap Sort Algorithm

The last week I was fighting with algorithms again. 🙂c#

Thus, I had to implement the mighty heap sort in C# and here is what I came up with. The idea of the code below is the following:

  1. In line 68 generate a list of unsorted numbers.
  2. The in line 70 create an object myHeap of type heap, taking the list as argument of the class.
  3. Thus, the class has two properties – myList and myLen (line 6 and 7), which are used in the constructor.
  4. The sorting is done with the function “heapsort” of the object heap (line 15).
  5. This function uses another one, adjust, to sort the list in the correct order. To get a better idea, it is advisable to use breakpoints.
  6. At the end, we simply print the list with a built-in function of the class heap.
  7. That’s all folks!

Here comes the code:

Enjoy it!


Tagged with: , ,