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?
- Public class Heapsort {
- private int[] numbers;
- private int length;
- Public Heapsort (int[] numbers) {
- this.numbers = numbers;
- this.length = numbers.length;
- }
- /**
- * Adjust binary tree
- * 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
- * The node number starts at 1, and the index in the corresponding array is number-1
- * @param nodeId node number, starting from 1
- */
- public void adjust (int nodeId) {
- int swapid;
- int flag = 0; //Do you need to continue to adjust downwards
- While (NodeId * 2 <= this.length && flag = = 0) {
- //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)
- int index = nodeId- 1; the index of the number in the array of the//node corresponding
- int leftchild = nodeId * 2- 1; the index of the number in the array of the///left Dial hand node
- int rightchild = nodeId * 2; //Right child node corresponding to the index of the number in the array
- if (Numbers[index] < Numbers[leftchild]) {
- Swapid = NodeId * 2;
- } Else {
- Swapid = nodeId;
- }
- //If there is a right child node, then compare with right child node
- if (nodeId * 2 + 1 <= this.length) {
- if (Numbers[swapid- 1] < Numbers[rightchild])
- Swapid = NodeId * 2 + 1;
- }
- //If the minimum node number is not self-explanatory, there is a smaller child node than the parent node
- if (swapid! = nodeId) {
- Swap (swapid, nodeId);
- NodeId = Swapid;
- } Else {
- flag = 1;
- }
- }
- }
- /**
- * Exchange values for two nodes
- * @param nodeId1
- * @param nodeId2
- */
- public void swap (int nodeId1, int nodeId2) {
- int t = numbers[nodeid1- 1];
- NUMBERS[NODEID1- 1] = Numbers[nodeid2- 1];
- Numbers[nodeid2- 1] = t;
- }
- /**
- * Create maximum heap
- */
- public void Createmaxheap () {
- //From the last non-leaf node to the first node, in turn, upward adjustment
- For (int i = this.length/ 2; I >= 1; i--) {
- adjust (i);
- }
- }
- public static void Main (string[] args) {
- int[] numbers = new int[] { 4, 3, 6, 2, 7, 1, 5};
- For (int x = 0; x < numbers.length; × x + +) {
- System.out.print (Numbers[x] + "");
- }
- System.out.println ();
- Heapsort heap = new Heapsort (numbers);
- Heap.createmaxheap ();
- }
- }
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?
- Public void Sort () {
- While (this.length > 1) {
- Swap (1, this.length);
- this.length--;
- Adjust (1);
- }
- For (int x = 0; x < numbers.length; × x + +) {
- System.out.print (Numbers[x] + "");
- }
- }
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