Java Implementation---Heap sorting heap sort

Source: Internet
Author: User

Heap Sort and quick sort , merge sort are the common sort methods of time complexity O (N*LOGN). Before learning heap sequencing, let's first explain what is a two-fork heap in the data structure.

Definition of a heap

The sequence of n elements, {k1,k2,...,kn}, is called a heap when and only if one of the following relationships is met.

Case 1:ki <= k2i and Ki <= k2i+1 ( minimized heap or small top heap )

Case 2:ki >= k2i and Ki >= k2i+1 ( largest heap or large top heap )

where i=1,2,..., n/2 downward rounding;

If a one-dimensional array corresponding to this sequence (that is, a storage structure with a one-dimensional array for this sequence) is treated as a complete binary tree , the meaning of the heap indicates that the values of all non-terminal nodes in the complete binary tree are not greater than (or not less than) the values of their left and right child nodes.

Thus, if the sequence {k1,k2,...,kn} is a heap, the top element of the heap (or the root of a complete binary tree) must be the minimum (or maximum) of n elements in the sequence.

Storage of Heaps

  Generally use an array to represent the heap, the Joghen node exists ordinal 0, I node of the parent node subscript is (i-1)/2. The index of the left and right sub-nodes of the I node is 2*i+1 and 2*i+2 respectively.

Implementation of Heap sorting

 The implementation of heap sequencing requires two issues to be resolved:

1. How to build a heap from an unordered sequence?

2. How do I adjust the remaining elements to become a new heap after the top element of the output heap?

Consider the second problem, usually after the output heap top element, is considered to exclude this element, and then use the last element of the table to fill its position, from the top down to adjust: first, the heap of the topmost element and its left and right subtree of the root node to compare, the smallest elements exchanged to the top of the heap, and then along the path of the Until the leaves knot, you get a new heap.

We call this "screening" the adjustment process from the top of the heap to the leaves.

The process of building a heap from an unordered sequence is a process of "filtering" over and over again.

Package heapsort;/** * Heap sorting, sequential storage * Large heap * @author Super Boy * */public class HeapSort2 {int[] arr;public static void Main (Stri  Ng[] args) {//TODO auto-generated method StubHeapSort2 heapsor = new HeapSort2 (); int[] arr = {7,23,45,9,40,73,12,49}; 0 subscript is the array length, Heapsor.arr = Arr;heapsor.heapsort (arr), and for (int i=0;i<arr.length;i++) System.out.print ("..." +arr[i]);} void Heapadjust (int[] arr,int s,int m) {//known ARR[S...M] In addition to Arr[s] is the definition of the heap, this function adjusts arr[s] keyword, so that arr[s...m] becomes a maximum heap int rc = Arr[s];  S is the last non-leaf node for (int j=2*s;j <= m;j*=2) {if (j<m && arr[j]<arr[j+1]) j + +;     J is the key of the larger subscript if (RC >= arr[j]) break;  Arr[s] = Arr[j]; Move up to parent node s=j;}  ARR[S]=RC; Place to put}void Heapsort (int[] arr) {//heap sort on order table for (int i= (arr.length-1)/2;i > 0;i--) {//Because 0 is not used,  So Length-1heapadjust (arr,i,arr.length-1); }system.out.println ("..."); Outputarr (1); for (int i=arr.length-1;;;;); ". (int.)"; "(") ";". " i>1; i--) {int temp = arr[1];arr[1] = arr[i];arr[i] = temp;heapadjust (arr,1,i-1);}} void Outputarr (int i) {if(i <= arr[0])  {System.out.println (arr[i]); Outputarr (i*2);  Left Outputarr (i*2+1); Right}}

  Operation Result:

.....................
73
45
40
23
49
12
9
.. 7..9..12..23..40..45..49..73

Heap sequencing analysis

Heap sorting method is not worth advocating for files with fewer records, but it is still very effective for n larger files. Because its run time is mainly spent on the initial heap under construction and the adjustment of the new heap when the repeated "filtering".

Heap sequencing in the worst case, the time complexity is also O (NLOGN). This is the greatest advantage of heap sorting relative to fast sorting. In addition, heap ordering requires only one record-size secondary storage space for Exchange.

Java Implementation---Heap sorting heap sort

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.