006--heap Sorting by decomposing method

Source: Internet
Author: User

Heap ordering is ordered using the priority queue implemented by the two fork heap.

Introduce several concepts first:

The first is the priority queue, which is a data structure that supports two actions: deleting the largest element and inserting the element.

The second is a two-fork heap, and the binary heap is a data structure that is able to implement the basic operation of the priority queue with that red. It uses an array to hold the data, and in this array, each element is guaranteed to be greater than or equal to two other elements of a particular position. Accordingly, the elements of these locations will have at least one island equal to another two elements in the array, and so on.

If you draw all the elements into a binary tree, you can see the structure by connecting each of the larger elements with the two smaller elements.

It is generally represented by a complete binary tree.

Let's get to the bottom and give the complete code:

 PackageAsen.yang; Public classHeapsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//the first element of the array (index 0 position) is not used, so the assignment is min_value.         int[] A = {integer.min_value, 5, 1, 4, 8, 3, 9, 0, 2, 7, 6 }; Sort (a);//to sort//print output, starting from subscript 1         for(inti = 1; i < a.length; i++) {System.out.print (A[i]+ " "); }    }    //compares the size of the elements of two indexes in an array, or returns False if I>j returns True    Private Static BooleanLessint[] PQ,intIintj) {returnPq[i] <Pq[j]; }    //swaps the values of two indexed elements in an array    Private Static voidExch (int[] PQ,intIintj) {intt =Pq[i]; Pq[i]=Pq[j]; PQ[J]=T; }    //Floating Operation    Private Static voidSwimint[] PQ,intk) { while(k > 1 && less (PQ, K/2, K)) {Exch (PQ, K/2, K); K= K/2; }    }    //Sinking Operation    Private Static voidSinkint[] PQ,intKintN) { while(2 * k <=N) {//the two sub-nodes of K are 2k and 2k+1,            intj = 2 *K; if(J < N && Less (PQ, J, J + 1)) {                //here to determine the size of 2k and 2k+1, select the larger elementJ + +; }            //if the K (parent) node is greater than or equal to J (child) nodes, the state has been adjusted to an orderly            if(!Less (PQ, K, J)) {                 Break; }                        //otherwise, the K (parent) node and the J (sub) node are exchanged to make the order .Exch (PQ, K, J); //the other J (sub) node becomes the new comparison object .K =J; }    }     Public Static voidSortint[] a) {//array available length is actual length-1        intN = a.length-1; //Build Heap         for(intK = N/2; K >= 1; k--) {sink (A, k, N); }                //Heap Sort         while(N > 1) {Exch (A,1, n--); Sink (A,1, N); }    }}

The key is the sink method, which is the function of the heap in an orderly state because a single is becoming smaller than its two sub-nodes, or one of them, can be broken by swapping it with the larger of its two sub-nodes to recover the heap.

The exchange may continue to break the ordered state of the heap at the child's point, so it needs to be repaired in the same way, moving the node down until its sub-nodes are smaller or reaching the bottom of the heap.

The final sort method implements the overall process of sorting.

This is to construct the sub-heap from right to left with the sink function. You only need to scan half of the elements in the array at first, because you can skip the sub-heap of size 1. Finally, the sink method is called on position 1, and the scan ends.

The next while Loop a[1] and a[n] swap and fix the heap, so repeat until the heap becomes empty.

The result of the final printing is:

is consistent with the expectations.

006--heap Sorting by decomposing method

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.