Write the algorithm. Sort. heapsort by yourself

Source: Internet
Author: User

Heap Sort Algorithm

Heap-Sort uses Max heap data structure to sort a sequence. The steps are:

  • We need construct a max heap in place firstly, so that we can make sure that the bigest value locates on the first position of the heap.
  • Swap the top value with the last value of the sequence, and shrink the size to nSize-1, and reconstruct the max heap.
  • Swap loopsly and the sequence will be sorted.

The key issue is how to present a max heap data structure by an array.

 

  1. # Define left_child_index (idx) (idx * 2 + 1)
  2. # Define right_child_index (idx) (idx * 2 + 2)
  3. # Define parent_index (idx) (idx-1)/2
  4. /*
  5. Given a max-heap, if we changed a value at idx, We shoshould make sure
  6. That all its children are not bigger than it.
  7. */
  8. Void max_heapify (const int idx, int arr [], const int nsize)
  9. {
  10. Int ncur = idx;
  11. Int nleft = left_child_index (ncur );
  12. Int nright = right_child_index (ncur );
  13. If (nright <nSize-1)
  14. {
  15. If (ARR [ncur] <arr [nright])
  16. {
  17. STD: swap (ARR [ncur], arr [nright]);
  18. Max_heapify (nright, arr, nsize );
  19. }
  20. }
  21. If (nleft <nSize-1)
  22. {
  23. If (ARR [ncur] <arr [nleft])
  24. {
  25. STD: swap (ARR [ncur], arr [nleft]);
  26. Max_heapify (nleft, arr, nsize );
  27. }
  28. }
  29. }
  30. /*
  31. A Non Complete Binary Tree has (nsize + 1)/2 children
  32. */
  33. Void build_max_heap (INT arr [], const int nsize)
  34. {
  35. Int ncount = nsize-(nsize + 1)/2;
  36. For (INT I = 0; I <ncount; ++ I)
  37. {
  38. Max_heapify (I, arr, nsize );
  39. }
  40. }
  41. /*
  42. Build a max heap firstly, so the first one is the biggest one
  43. Swap with the last one, rebuild the max heap, but ignoring the last one
  44. */
  45. Void sortheap: Sort (INT arr [], const int nsize)
  46. {
  47. Alog: Print (_ T ("# Heap Sort :"));
  48. Alog: Print (ARR, nsize );
  49. Build_max_heap (ARR, nsize );
  50. Alog: Print (ARR, nsize );
  51. For (INT I = nSize-1; I> 1; -- I)
  52. {
  53. STD: swap (ARR [0], arr [I]);
  54. Max_heapify (0, arr, I );
  55. Alog: Print (ARR, nsize );
  56. }
  57. }
  58. Void sortheap: Test ()
  59. {
  60. Int arr [] = {2, 9, 3, 7, 8, 6, 4, 5, 0, 1 };
  61. Const int nsize = sizeof (ARR)/sizeof (INT );
  62. Sortheap: Sort (ARR, nsize );
  63. }

 

The test result:

 

# Heap Sort:

2 9 3 7 8 6 4 5 0 1

======================
9 8 6 7 3 4 2 5 0 1
8 7 4 6 3 2 1 5 0 9
7 6 2 4 3 1 0 5 8 9
6 5 2 4 3 1 0 7 8 9
5 4 0 3 2 1 6 7 8 9
4 3 0 1 2 5 6 7 8 9
3 2 0 1 4 5 6 7 8 9
2 1 0 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.