Sorting algorithm-------Heap Ordering

Source: Internet
Author: User

For n elements of sequence {R0, R1, ..., Rn} when and only if one of the following relationships is met, it is called a heap:

(1) ri <= r2i+1 and ri <= r2i+2 ( Small top heap)

(2) ri >= r2i+1 and ri >= r2i+2 ( large top heap)

The heap is actually a complete binary tree of sequential storage

Complete binary tree:

Sequential storage:

Large Top pile:

Heap Sort:

The use of large top heap (small top heap) heap top record is the maximum keyword (minimum keyword) This feature, making it easy to select the maximum record (minimum record) from the disorder every time.

Its basic idea is (Big Top heap):

1) The initial order to sort the keyword sequence (r1,r2 .... Rn) is constructed into a large top heap, which is the initial no-need zone;

2) Swap the top element of the heap r[1] with the last element R[n] to get a new unordered area (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n];

3) due to the new heap top r[1] may violate the nature of the heap, it is necessary to the current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete.

The first step is how to initialize an array to a large top heap:

How to sort it?

After sorting, the array becomes orderly

Package Com.neuedu.java;import java.util.Arrays;/** * Heap sort * @author WZC*/ Public classHeapsort { Public Static voidMain (string[] args) {int[]a={ -, -, -, -, the, -,Ten, -, +}; //call Heap Ordering methodHeapsort (a); //Output ArraySystem. out. println (Arrays.tostring (a)); }          /** Heap Sorting method * @param A to sort array*/      Public  Static voidHeapsort (int[] a) {          intend=a.length-1;//end Array The last number subscript//Initialize heap----> Large top heap           for(intI= (end-1)/2; i>=0; i--) {//start with the first node with a child node//Heap Adjustment---> large top pileHeadadjuest (A, I, end); }          //to sort           for(intJ=end;j>0; j--){              //1. The first number is exchanged with the last number              inttemp=a[0]; a[0]=A[j]; A[J]=temp; //2. After swapping, make the heap adjustment again---> Large top heapHeadadjuest (A,0, J-1); }     }     /** * Heap Adjustment method * @param an array to be adjusted * @param parent Current parent node subscript * @param end array subscript for the last number of */      Public Static voidHeadadjuest (int[] A,intParentintend) {         intTemp =a[parent];//Save the current parent node---facilitate subsequent comparisons and data exchange         intChild =2*parent+1;//gets the left child subscript of the current parent nodeSystem. out. println (temp); //determine if the left child exists          while(child<=end) {             if((child+1) <=end&&a[child]<a[child+1]) { child++;//determine if the right child is present and find the largest one in the child's node            }             //If the parent node is larger than the child node, there is no need to adjust and exit directly            if(temp>=A[child]) {                  Break; } A[parent]=a[child];//give the largest child to the parent nodeParent=child;//If the child node is also a parent node, in order to ensure that the children node after the exchange of data is larger than the kid's own kids node data,//also in the loop, when the child node has a parent node, and his own children node is being comparedChild=2*child+1; } A[parent]=temp;//Complete data exchange     }         }

Sorting algorithm-------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.