Implement heap sorting in java

Source: Internet
Author: User

Implement heap sorting in java

Recently, I started to learn about sorting. I plan to organize a sorting algorithm every day.

Today, I sorted out the heap sorting algorithm. At first I did not know the process of heap sorting.

Note the following:

When the coordinates of left and right children are calculated based on the Root Node I (I is the subscript in the array), left = I * 2 + 1, right = I * 2 + 2, because the root node starts from the array 0.

Import java. util. arrays;/*** heap sorting Introduction: * heap sorting is not recommended for files with a small number of records, but it is very effective for files with a large number of n. * In the worst case, the time complexity is O (nlogn). In the best case, the time complexity is O (1). * heap sorting is unstable. * *** Specific heap sorting process: * 1. First, compare the current node and its child node from the first non-leaf node, and place the largest element on the current node, swap the current node and the largest node element. * 2. Perform the 1 process for all the elements before the current element, so as to generate the maximum heap * 3. Swap the heap top element with the last element. The length of the list is reduced by 1. Therefore, the unordered area is reduced by 1, the ordered area is added with 1*4, the remaining elements are re-adjusted to build the heap * 5, and continue with 3 and 4, until all elements are sorted *****/public class HeapSort {public static void heapSort (int [] num) {if (num = null) return; int len = num. length; buildHeap (num, len);/** place the top element of the heap to the last position of the array each time */while (len> 1) {swap (num, len-1, 0); len --; adjustHeap (num, len, 0 );}} /** construct an unordered array into a large top heap */private static void buildHeap (int [] num, int len) {/** begin is the subscript of the last root node in the array */int begin = len/2-1;/** according Traverse each root node and adjust it to make each root node bigger than its child node */for (int I = begin; I> = 0; I --) {adjustHeap (num, len, I) ;}/ ** adjust the location of the root node and the child node, make it meet the requirements of the Big Top heap */private static void adjustHeap (int [] num, int len, int I) {/** obtain the subscript of the left child and right child of the root node whose subscript is I */int leftChild = I * 2 + 1; int rightChild = I * 2 + 2;/** mark the maximum subscript of this adjustment as I */int max = I; while (leftChild <len | rightChild <len) {/** if num [max]'s left child num [leftChild] is larger than max, save leftChild in max */if (leftCh Ild <len & num [max] <num [leftChild]) {max = leftChild;}/** if num [max]'s right child num [rightChild] is larger than max, save rightChild to max */if (rightChild <len & num [max] <num [rightChild]) {max = rightChild;}/** if the preceding operation is in progress, assignment occurred */if (I! = Max) {swap (num, max, I); I = max; leftChild = I * 2 + 1; rightChild = I * 2 + 2;} else {break ;}} // while (leftChild <len | rightChild <len)} // private static void adjustHeap () /** switch the positions of num [x] and num [y] in the array */private static void swap (int [] num, int x, int y) {num [x] = num [x] ^ num [y]; num [y] = num [x] ^ num [y]; num [x] = num [x] ^ num [y];} public static void main (String [] args) {int [] num = {0, 9, 7, 0, 0, 6,-40,200}; HeapSort. heapSort (num); System. out. println (Arrays. toString (num ));}}

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.