• We just launched and are currently in beta. Join us as we build and grow the community.

Demonstrating Heap Sort in C#

Zorophobe

Algorithm Innovator
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
57
Likes
19
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Now, let’s learn how to demonstrate heap sort program in C#. But, before anything else let’s know what heapsort is? Here’s a short definition of it, heapsort is a comparison-based sorting algorithm that uses the heap data structure. In this program, the topmost item (the largest) is removed first and stored in an array. Then, it is replaced by the rightmost leaf and re-established the heap. The same process is repeated until there are no more items left in the heap and the array is sorted. Let’s begin:
Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#.
csharp_create_app_6.png

Step 2

Do the form just like shown below.
heapsortform1.png

Step 3

Create a method for the heap sort.
  1. int

    [

    ]

    numbers =

    {

    2

    , 5

    , 1

    , 10

    , 6

    , 9

    , 3

    , 7

    , 4

    , 8

    }

    ;
  2. public

    void

    heapsort(

    )
  3. {
  4. int

    i, c;
  5. for

    (

    i =

    5

    ;

    i >=

    0

    ;

    i--

    )
  6. {
  7. adjust(

    i, 9

    )

    ;
  8. }
  9. for

    (

    i =

    8

    ;

    i >=

    0

    ;

    i--

    )
  10. {
  11. c=

    numbers[

    i +

    1

    ]

    ;
  12. numbers[

    i +

    1

    ]

    =

    numbers[

    0

    ]

    ;
  13. numbers[

    0

    ]

    =

    c;
  14. adjust(

    0

    , i)

    ;
  15. }
  16. }
  17. private

    void

    adjust(

    int

    i, int

    n)
  18. {
  19. int

    a, b;
  20. try
  21. {
  22. a =

    numbers[

    i]

    ;
  23. b =

    2

    *

    i;
  24. while

    (

    b <=

    n)
  25. {
  26. if

    (

    b <

    n &&

    numbers[

    b]

    <

    numbers[

    b +

    1

    ]

    )
  27. b++;
  28. if

    (

    a >=

    numbers[

    b]

    )
  29. break

    ;
  30. numbers[

    b /

    2

    ]

    =

    numbers[

    b]

    ;
  31. b *=

    2

    ;
  32. }
  33. numbers[

    b /

    2

    ]

    =

    a;
  34. }
  35. catch

    (

    IndexOutOfRangeException e)
  36. {
  37. listBox1.

    Items

    .

    Add

    (

    "Array Out of Bounds "

    +

    e)

    ;
  38. }
  39. }
  40. public

    void

    display(

    )
  41. {
  42. for

    (

    int

    i =

    0

    ;

    i <

    10

    ;

    i++

    )
  43. {
  44. listBox1.

    Items

    .

    Add

    (

    "{"

    +

    i++

    +

    "}"

    +

    numbers[

    i]

    )

    ;
  45. }

  46. }

Step 4

Double click the button and call the method that you have created for the heap sort in the click event handler of the button to execute when the button is click.
  1. listBox1.

    Items

    .

    Clear

    (

    )

    ;
  2. listBox1.

    Items

    .

    Add

    (

    "Array Elements Before sorting : "

    )

    ;
  3. display(

    )

    ;
  4. heapsort(

    )

    ;
  5. listBox1.

    Items

    .

    Add

    (

    "Array Elements After sorting : "

    )

    ;
  6. display(

    )

    ;

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

 

452,496

335,994

336,002

Top