Sort algorithm Four: heap sort Heapsort

Source: Internet
Author: User

1 /**2 * Heap sequencing ideas: O (NLOGN)3 * Use the maximum heap, pass in an array, first use the number to build the heap, maintain the nature of the heap4 * Replace the first number with the last number in the heap, because the first number is the largest5 * Reduce the size of the heap by one6 * Then maintain the heap's nature on the heap size7 * Repeat Operation :8  *    9  *    Ten  */ One  Public  classHeapsort A { -     /** - * Static variable holds the size of the heap the      */ -     Private  Static  intheapsize = 0 ;  -  -     /** + * Heap Sort Master Method - * Build the largest heap and then sort the heap + * Heap sequencing is to place the topmost largest element at the bottom and then maintain the properties of the maximum heap above A      */  at      Public  Static  voidHeapsort (int[] resoucearr) -     { -         //the size of the heap -HeapSize = resoucearr.length-1 ; -  - _buildmaxheap (Resoucearr); in  -          for(inti = resoucearr.length-1; I >= 0; i--) to         { +             inttemp = resoucearr[0] ; -Resoucearr[0] =Resoucearr[i]; theResoucearr[i] =temp; *              $HeapSize = heapsize-1 ; Panax Notoginseng_maxheapify (Resoucearr, 0 );  -         } the     } +  A     /** the * Build Maximum heap + * After construction is not orderly, but in line with the maximum heap nature, the above number must be greater than the number below -      */ $     Private  Static  void_buildmaxheap (int[] arr) $     { -         intLength = Arr.length-1 ;  -  the         //starting from the penultimate row to maintain the maximum heap properties -         //when HeapSize is an even number, heapsize is reduced by oneWuyi         //when the heapsize is odd, it does not change the         if(length% 2 = = 0) -         { Wulength--; -         } About          for(inti = LENGTH/2; I >= 0; i--) $         { - _maxheapify (arr, i); -         } -     } A  +     /** the * For maintaining the nature of the heap - * Tree-shaped heap in postion position down to maintain the nature of the heap, from postion down to meet the maximum heap properties $      */ the     Private  Static  void_maxheapify (int[] arr,intpostion) the     { the         //calculate Postion's left child and right child thepostion = postion + 1 ; -  in         intL = postion * 2-1;  the         intr = postion * 2 ; thepostion = postion-1 ; About  the         intlargest =Maxnuminthreenum (arr, postion, L, R); the  the         //if the maximum heap property is not met, the value is exchanged and then the maximum heap property is checked under the tree +         if(Largest <=heapsize) -         {     the             if(Largest! =postion)Bayi             { the                 //Exchange maximum value with parent node value the                 inttemp =Arr[postion]; -Arr[postion] =Arr[largest]; -Arr[largest] =temp; the                 //If the child node changes, rebuild the largest heap that has child nodes as root the _maxheapify (arr, largest);  the  the             } -         } the     }     the  the     /**94 * Compare three numbers in an array to find the maximum value the      */ the     Private  Static  intMaxnuminthreenum (int[] arr,intAintBintc) the     {98         intMax =A; About         //the array length is smaller than the left child, and the maximum value is itself -         if(HeapSize <b)101         {102Max =A;103         }104         Else if(HeapSize >=b && heapsize <c) the         {106             if(Arr[a] <Arr[b])107             {108Max =b;109             } the         }111         Else the         {113             if(Arr[a] >Arr[b]) the             { theMax =A; the             }117             Else118             {119Max =b; -             }121             if(Arr[max] <Arr[c])122             {123Max =C;124             }             the         }126         returnMax;127     }     -}

Sort algorithm Four: heap sort Heapsort

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.