Java Sorting Algorithm _ 013 heapsort)

Source: Internet
Author: User
Package com. arithmetic; // heapsort is a sort algorithm designed by using the data structure of heap. A heap is a structure similar to a fully binary tree. // It satisfies the heap nature at the same time. That is, the key value or index of a subnode is always smaller than or greater than its parent node. Public class test_wzs013 {Private Static int [] Sort = new int [] {10, 3, 5, 7, 9, 1, 4, 2, 6, 8 }; public static void main (string [] ARGs) {buildmaxheapify (SORT); heapsort (SORT); print (SORT);} Private Static void buildmaxheapify (INT [] data) {// create the maximum heap only when no child node exists. Int startindex = getparentindex (data. length-1); // create the maximum heap from the end. The heap is correct for (INT I = startindex; I> = 0; I --) {maxheapify (data, data. len Required, I) ;}/ ***** create the maximum heap ** @ Param data * @ Param heapsize * You need to create the maximum heap size, which is generally used in sort, because the maximum value is placed at the end, the end is no longer classified as the maximum heap * @ Param Index * the location where the maximum heap needs to be created */Private Static void maxheapify (INT [] data, int heapsize, int index) {// compare the current vertex with the Left and Right subnodes: int left = getchildleftindex (INDEX); int right = getchildrightindex (INDEX); int largest = index; if (left 

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.