# 6th Heap Sorting Java implementation of the simplest version

Source: Internet
Author: User

Only the sort of the int type is considered, and the generic implementation is considered later.

`1  Public classHeap {2     3      Public intHeap_size;//initialized in Build_max_heap, automatically called by Heap_sort4     5      Public intParentinti) {6         return(i-1)/2;7     }8      Public intLeftinti) {9         return2 * i + 1;Ten     } One      Public intRightinti) { A         return2 * i + 2; -     } -      the     /**keep the nature of the heap, - * Assuming left (i), right (i) is the root of the two binary tree is the largest heap, - * A[i] may be smaller than its children, thus violating the maximum heap properties - * This function allows a[i] to "descend" in the maximum heap, making the subtree with the root of I as the largest heap +      * */ -      Public voidMax_heapify (int[] A,inti) { +         intL =Left (i); A         intR =Right (i); at         intlargest; -         //from A[i] A[left (i)] a[right (i)] to find the largest, subscript exists in largest -         //if A[i] is the largest, then it is assumed that I is the root of the subtree is already the largest heap, function end -         //otherwise interchange a[i] and A[largest], the value of the node labeled largest after the interchange is A[i] -         //The subtree of the node may violate the maximum heap, so recursive invocation of largest subscript -         if(L < heap_size && A[l] >A[i]) { inlargest =l; -         }  to         Else{ +largest =i; -         }     the         if(R < heap_size && A[r] >A[largest]) { *largest =R; \$         }Panax Notoginseng         if(Largest! =i) { -             intTMP =A[i]; theA[i] =A[largest]; +A[largest] =tmp; A              the max_heapify (A, largest); +         } -     } \$      \$     /** - * Build a heap - * Bottom-up array A[1..N] (here N=length[a]) becomes the maximum heap the * Sub-array a[(Floor (N/2) + 1): Elements in the tree are all leaves - * So the process is called once for each non-leaf nodeWuyi      * */ the      Public voidBuild_max_heap (int[] A) { -Heap_size = A.length;//Initialize Heap_size Wu          for(inti = (HEAP_SIZE/2-1); i >-1; --i) { - max_heapify (A, i); About         } \$     } -      -     /** - * Heap Sorting algorithm A * First build the largest heap, root a[1] is the largest, then displace to the correct position of the array a[n] + * Remove nodes from the heap n,a[1..n-1] built the largest heap, but the new root may violate the maximum heap properties, the * The call max_heapify (a,1) can maintain the property at this time, constructing the maximum heap in a[1..n-1].  - * Repeat, heap size reduced from n-1 to 2 \$      * */ the      Public voidHeapsort (int[] A) { the build_max_heap (A); the          for(inti = a.length-1; i > 0; --i) { the             intTMP = A[0]; -A[0] =A[i]; inA[i] =tmp; the--heap_size; theMax_heapify (A, 0); About         } the     } the      the      Public Static voidShow_array (int[] A) { +          for(inti = 0; i < a.length; ++i) { -System.out.print (A[i] + "."); the         }Bayi System.out.println (); the     } the      -      Public Static voidMain (string[] args) { -         int[] A = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; the          the          theSystem.out.println ("Before Heapsort:"); the Show_array (A); -          theHeap h =NewHeap (); the H.heapsort (A); the         94System.out.println ("After Heapsort:"); the Show_array (A); the          the     }98      About}`

Operation Result:

6th Heap Sorting Java implementation of the simplest version

Related Keywords:

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.