Java and Algorithm (8)-Heap ordering

Source: Internet
Author: User

A heap is a special complete binary tree, which is characterized by that all parent nodes are smaller than child nodes, or that all parent nodes are larger than byte points. The first is called the minimum heap, and the latter is called the maximum heap.

For example, the following two:

So what's the effect of this feature? Since the title is a heap sort, it can certainly be used for sorting. Want to sort by heap first to create a heap, if the 4 3 6 2 7 1 5 of these seven numbers are ordered from small to large, you need to create a maximum heap with these seven numbers, to see the code:

[Java]View PlainCopyprint?
  1. Public class Heapsort {
  2. private int[] numbers;
  3. private int length;
  4. Public Heapsort (int[] numbers) {
  5. this.numbers = numbers;
  6. this.length = numbers.length;
  7. }
  8. /** 
  9. * Adjust binary tree
  10. * If the parent node number is x, then the number of the left Dial hand node is 2x and the right child node number is 2x+1
  11. * The node number starts at 1, and the index in the corresponding array is number-1
  12. * @param nodeId node number, starting from 1
  13. */
  14. public void adjust (int nodeId) {
  15. int swapid;
  16. int flag = 0; //Do you need to continue to adjust downwards
  17. While (NodeId * 2 <= this.length && flag = = 0) {
  18. //First determine the relationship between it and the left child node and use the Swapid to record the node number with a smaller value (the maximum heap is a larger record)
  19. int index = nodeId- 1; the index of the number in the array of the//node corresponding
  20. int leftchild = nodeId * 2- 1; the index of the number in the array of the///left Dial hand node
  21. int rightchild = nodeId * 2; //Right child node corresponding to the index of the number in the array
  22. if (Numbers[index] < Numbers[leftchild]) {
  23. Swapid = NodeId * 2;
  24. } Else {
  25. Swapid = nodeId;
  26. }
  27. //If there is a right child node, then compare with right child node
  28. if (nodeId * 2 + 1 <= this.length) {
  29. if (Numbers[swapid- 1] < Numbers[rightchild])
  30. Swapid = NodeId * 2 + 1;
  31. }
  32. //If the minimum node number is not self-explanatory, there is a smaller child node than the parent node
  33. if (swapid! = nodeId) {
  34. Swap (swapid, nodeId);
  35. NodeId = Swapid;
  36. } Else {
  37. flag = 1;
  38. }
  39. }
  40. }
  41. /** 
  42. * Exchange values for two nodes
  43. * @param nodeId1
  44. * @param nodeId2
  45. */
  46. public void swap (int nodeId1, int nodeId2) {
  47. int t = numbers[nodeid1- 1];
  48. NUMBERS[NODEID1- 1] = Numbers[nodeid2- 1];
  49. Numbers[nodeid2- 1] = t;
  50. }
  51. /** 
  52. * Create maximum heap
  53. */
  54. public void Createmaxheap () {
  55. //From the last non-leaf node to the first node, in turn, upward adjustment
  56. For (int i = this.length/ 2; I >= 1; i--) {
  57. adjust (i);
  58. }
  59. }
  60. public static void Main (string[] args) {
  61. int[] numbers = new int[] { 4, 3, 6, 2, 7, 1, 5};
  62. For (int x = 0; x < numbers.length; × x + +) {
  63. System.out.print (Numbers[x] + "");
  64. }
  65. System.out.println ();
  66. Heapsort heap = new Heapsort (numbers);
  67. Heap.createmaxheap ();
  68. }
  69. }

For this example, the series, from THIS.LENGTH/2 to 1, performed a total of round loops.

First round:

Second round:


Third round:

After the adjustment is complete, the current two-fork tree already conforms to the maximum heap characteristics and can be used to sort from small to large. The principle of heap sorting is to swap the number of the top and last node of the heap, that is, put the largest number at the end of the array, and then re-perform the adjustment process with the number of first n-1 except the maximum number to conform to the maximum heap characteristics. Repeat the process until only one number is left in the heap.

[Java]View PlainCopyprint?
  1. Public void Sort () {
  2. While (this.length > 1) {
  3. Swap (1, this.length);
  4. this.length--;
  5. Adjust (1);
  6. }
  7. For (int x = 0; x < numbers.length; × x + +) {
  8. System.out.print (Numbers[x] + "");
  9. }
  10. }

The time complexity of heap sequencing is the same as the average time complexity for fast sorting, which is O (Nlogn).

Java and Algorithm (8)-Heap ordering

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.