Algorithm (fourth edition) Learning Notes Java implementation heap sorting

Source: Internet
Author: User
Tags comparable

Following the previous implementation of the heap-based priority queue, this time a classic and elegant sorting algorithm, called heap ordering, is implemented using the last-completed heap-based priority queue capable of repeatedly deleting maximum element operations.

Heap sorting can be divided into two stages:

1. Build heap: During the build of the heap, we reorganize the original array into a heap;

2. Sinking sort: Remove all elements from the heap in descending order and get sorted results

The specific idea in the following code has a more detailed comment:

/** * * @author seabear * */public class Heapsort {/** * 1. Construction Dagen: Similar to the previous heap-based priority queue, the original array is re-organized into a heap-based priority queue with the maximum element deduplication operation; * 2. Sinking Sorting: Depending on the large heap that is constructed, it is easy to delete the largest element in the heap, and then put it in the empty position of the array after the heap has shrunk. * @param a */public static void sort (comparable[] a) {int len = a.length-1;//constructs Dagen//skips a heap of only one node, which is a heap of size 1, starts scanning from the middle of the array, calls Sink () method, the layer is decremented, and the last call to the sink () method at 1 position ends. The purpose of this scan is to construct an ordered array of arrays and make the largest elements at the beginning of the array (the second largest element is near) for (int k = LEN/2; k >= 1; k--) {sink (A,k,len); show (a);} System.out.println ("sinking Start");//Sinking sort//1. Each sort first swaps the largest element with the last element, and then shrinks the array, sorting out the heap that dropped the last element//2. Sort the narrowed array, If the array length is greater than 1, jump to the first step to continue while (Len > 1) {exch (a,1,len--); sink (A,1,len); show (A);}} Sinking sort private static void sink (comparable[] a,int i,int len) {while (i*2 <= len) {int J = i * 2;if (J < Len && L ESS (A[j],a[j+1])) {j + +;} if (!less (A[i],a[j])) {break;} Exch (a,i,j); i = J;}} private static Boolean less (comparable v,comparable W) {return V.compareto (W) < 0;} private static void Exch (comparable[] v,int i, Int j) {Comparable temp = v[i];v[i] = v[j];v[j] = temp;} public static void SHow (comparable[] a) {for (int i = 1; i < a.length; i++) {System.out.print (A[i] + "");} System.out.println ();} public static void Main (string[] args) {int N = 12;integer[] A = new Integer[n];for (int i = 1; I <= N-1; i++) {A[i] = (in T) (Math.random () * 10 + 1);} Show (a); sort (a); show (A);}}


Heap sequencing plays an important role in the study of sequencing complexity, as it is the only way we know to be able to use space and time optimally, even in the worst case, to ensure the use of ~2NLGN and constant additional space. Often used in embedded systems or in low-cost mobile devices (systems with very tight space), but many applications of modern systems seldom use it, because it cannot take advantage of caching. Array elements are rarely compared with other adjacent elements, so the number of cache misses is much higher than the algorithm that most comparisons make between adjacent elements, such as quick sort, merge sort, or even hill sort. On the other hand, the heap-implemented priority queue is becoming increasingly important in modern applications because it guarantees a number of levels of uptime in dynamic scenarios where the insertion operation and the removal of the maximum element operation are mixed.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Algorithm (fourth edition) Learning Notes Java implementation heap sorting

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.