Heap Sort Java Implementation

Source: Internet
Author: User

Heap Concept:

is a complete binary tree structure, divided into Dagen and small Gan, each non-leaf node is greater than (Dagen is greater than, small Gan thing less than) its two child nodes.

Heap Sort: The steps are divided into three stages:

1: Initialize large Gan (Keng Gen can also, this article takes Dagen as an example)

2: Swap heap top Max and array last,

3: Large-root heap adjustment for heap after swap position

4: Cycle through 2 steps, swap heap top and array second to last, third, etc. until the first place ...

Time Complexity Analysis:

Initialize heap sort structure time complexity requires N * LOGN

Maintenance of the new large-root heap complexity after each exchange is LOGN, N-Traversal switching, so the total time complexity: N * logn

The code is as follows

1  Packagecom.secbro.test;2 3 Importjava.util.Arrays;4 5  Public classHeapsort {6 7     //Test8      Public Static voidMain (string[] args) {9         int[] num = {10, 23, 17, 88, 39, 13, 11, 109, 108, 383, 100, 1, 10, 14};TenInitbuildheap (num, 0, num.length-1); OneSYSTEM.OUT.PRINTLN ("Initialize the big root heap:" +arrays.tostring (num)); A          for(inti = num.length-1; I >= 0; i--) { -Swap (num, 0, i); -Rebuildheap (num, 0, i); the         } -System.out.println ("Heap sort result:" +arrays.tostring (num)); -     } -  +     //Build Initialize large heap This step of time complexity is: N (LOGN) -     Private Static voidInitbuildheap (int[] num,intIndexintend) { +  A         if(num = =NULL|| Index >end) { at             return; -         } -          for(inti = end; I >= index; i--) { -             intParent = (i-1)/2; -             if(Num[i] >Num[parent]) { - swap (num, I, parent); in initbuildheap (num, index, i); -             } to         } +     } -  the     //Maintaining a new large-root heap structure *     Private Static voidRebuildheap (int[] num,intIndexintend) { $         if(num = =NULL|| Index >end) {Panax Notoginseng             return; -         } the         intleft = 2 * index + 1; +         intright = 2 * index + 2; A         if(Left < end && Num[index] <Num[left]) { the swap (num, index, left); + rebuildheap (num, left, end); -         } $         if(Right < end && Num[index] <Num[right]) { $ swap (num, index, right); - rebuildheap (num, right, end); -         } the     } - Wuyi     //Num[left] and Num[right] positions in the interactive array the     Private Static voidSwapint[] num,intLeftintRight ) { -         inttemp =Num[left]; WuNum[left] =Num[right]; -Num[right] =temp; About     } $}

Print results

Initialize the large root heap: [383, 109, 1, 108, 10, 1,,, 11, 13,, +, +,, 14, 17, 10, +, +/- 00, 108, 109, 383]

Heap Sort Java Implementation

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.