Java algorithm start heap sort instance _java

Source: Internet
Author: User
Tags int size

To learn heap sorting, first you need to understand the concept of the heap, which is an array. Can be approximated as an array storage method of a complete binary tree. But with him there are other properties, which are similar to binary sort trees. With the maximum heap and the minimum heap, the maximum heap is the value of the root node is greater than the child node, and the smallest heap is the value of the root node less than its child nodes. Heap sorting typically uses the largest heap, while the smallest heap constructs the precedence queue. There is a method in the heap to maintain the nature of the heap, the Maxheap method in our code below, which is the method of maintaining the maximum heap properties, the first parameter is the heap is the array, and the second parameter is the specific node position of the adjustment heap, possibly the value of this node does not conform to the nature of the maximum heap, So this is a worthwhile location as a parameter, and size is actually the number of valid elements stored in the heap. The length of the array may be the number of elements actually stored in the heap, but not necessarily all of the data we need to build the maximum heap. Introduction to the algorithm to get the Zoozi node is 2xi but our array is counted from 0, so the Zoozi node becomes the 2xi+1,buildheap is to build a maximum heap, we go to 2 per cent of the length of the reason is that these points of the child nodes are leaf nodes, So we build a maximum heap by sorting from the bottom up. This ensures that all nodes in our heap satisfy the maximum heap properties. The last heap sort is to remove the first node, then heap the remaining nodes, and then remove the root node. This ensures that each time we take out the maximum value.

Copy Code code as follows:

public class Heapsort {
private int GetLeft (int i) {
return 2*i+1;
}
private int GetRight (int i) {
return 2*i+2;
}
public void Maxheap (int[] heap,int loc,int size) {
int L=getleft (LOC);
int r=getright (LOC);
int largest=loc;
if (L<size&&heap[l]>heap[loc]) {
Largest=l;
}
if (R<size&&heap[r]>heap[largest]) {
Largest=r;
}
if (Largest!=loc) {
int Cache=heap[loc];
Heap[loc]=heap[largest];
Heap[largest]=cache;
Maxheap (heap, largest, size);
}
}
public void Buildheap (int[] heap) {
for (int i=heap.length/2;i>=0;i--) {
Maxheap (Heap, I, heap.length);
}
}
public void sort (int[] heap) {
Buildheap (heap);
for (int i=heap.length-1;i>1;i--) {
int cache=heap[0];
Heap[0]=heap[i];
Heap[i]=cache;
Maxheap (heap, 0,i);
}
int cache=heap[0];
HEAP[0]=HEAP[1];
Heap[1]=cache;

}

public static void Main (string[] args) {
Int[] Heap=new int[]{4,1,5,3,7,12,65,7};
Heapsort hs=new heapsort ();
Hs.sort (heap);
for (int i = 0; i < heap.length; i++) {
System.out.println (Heap[i]);
}
}
}

Related Article

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.