Heap Sort Java Implementation

Source: Internet
Author: User
Tags stub

Read a lot of bloggers wrote the principle of heap sorting, are explained quite clear, is the code implementation (mainly the Java language) some people dazzling. I have re-organized the code implementation of the heap sort (Java).

What are the problems and irregularities, but also hope that the partners remind me to correct them in time. Thank!!

Heap Sort Explanation: 73105580

77582627

Summary points:

(1) heap sequencing is not stable.

(2) Get increment sequence with large top heap, get descending sequence with small top heap.

(3) Sequencing time complex O (nlog2n), auxiliary space O (1).

(4) Initial build heap O (n)

1  Public classHeaptest {2 3      Public Static voidMain (string[] args) {4         int[] Array = {1,19,3,24,26,4,78,14,27,10,177,6,22,17,22,53};5 heapsort (array);6           for(inti = 0; i < Array.Length; i++) {7System.out.print (array[i]+ "");8         }9     }Ten  One     /** A * Heap Sorting -      * @paramArray -      */ the     Private Static voidHeapsort (int[] Array) { -         //TODO auto-generated Method Stub -         //consider boundary conditions -         if(Array.Length <= 1) { +             return; -         } +         //Building the initial heap A          for(inti = (ARRAY.LENGTH/2)-1; I >= 0; i--){ atAdjustheap (array,i,array.length-1); -         } -         //Replace the top element of the heap with the last of the array to be sorted, and heap adjust the new-length array -          for(intK = array.length-1; K >= 0; k--){ -             intTMP =Array[k]; -Array[k] = array[0]; inArray[0] =tmp; -             //here Adjustheap K represents the length of the array to be sorted, not the array subscript toAdjustheap (array, 0, k-1); +         } -     } the  *     /** $ * Build HeapPanax Notoginseng * Start is the following table for the starting element of the array to be adjusted, and end is the subscript for the last element of the array to be adjusted -      * @paramArray the      * @paramI +      * @paramlength A      */ the     Private Static voidAdjustheap (int[] Array,intStartintend) { +         //TODO auto-generated Method Stub -          $         inttemp,j; $         //Temp Stores the value at start -temp =Array[start]; -         //record the start left child's subscript with J: j = 2 * start + 1 the          for(j = 2 * start + 1;j <= end; j = J * 2 + 1){ -             //If start has right child, and right child is bigger than left child, J + +, get right child subscriptWuyi             if(j + 1 <= end && Array[j] < Array[j + 1]) { theJ + +; -             } Wu             //Judging when temp is in its present position (the current start position), is not larger than his child, and if so, adjust it to jump out of the adjustment loop; if not, the current child's value is assigned to the current start position -             if(Temp >=Array[j]) { About                  Break; $             } -Array[start] =Array[j]; -             //The subscript of the node in which the temp value can be assigned -Start =J; A         } +         //assigns temp to the current start position theArray[start] =temp; -     } $}

Heap Sort Java Implementation

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.