Heap sort _java of Java sort algorithm summary

Source: Internet
Author: User

This article illustrates the heap ordering of the summary of Java sorting algorithms. Share to everyone for your reference. The specific analysis is as follows:

1991 Computer Pioneer Laureate, Stanford University computer science professor Robert Floyd (Robert W.). Floyd) and Williams (J. Williams) jointly invented the famous heap sorting algorithm (Heap sort) in 1964. This article mainly introduces the heap sort to be implemented in Java.

Stacking sort (heapsort) is a sort algorithm designed using the data structure of a stacked tree (heap), which can quickly locate the elements of a specified index using the characteristics of an array. Heap ordering is an unstable sort method, the auxiliary space is O (1), the worst time complexity is O (nlog2n), and the average performance of heap order is close to the worst performance.

Heap sorting utilizes the feature of the maximum (or minimum) of the keywords in the Dagen (or small Gan) heap top record, making it easy to select the record of the largest (or smallest) keyword in the current unordered area.

(1) The basic idea of sorting with Dagen

① first the initial file R[1..N] into a large heap, which is the initial unordered area
② the maximum record r[1] (that is, the heap top) and the last record of the unordered area R[n] exchanged, thus obtaining the new unordered region R[1..n-1 and ordered region R[n], and satisfying the R[1..n-1].keys≤r[n].key
③ the current unordered area r[1..n-1] should be adjusted to a heap because the new root r[1 may violate the heap property after swapping. The r[1..n-1] is then exchanged with the largest record of the keyword r[1] and the last record of the interval r[n-1], resulting in a new unordered region r[1..n-2 and an ordered area r[n-1. N], and still satisfies the relationship r[1..n-2].keys≤r[n-1 ... N].keys, also adjust the r[1..n-2] to the heap.
......
Until the unordered area has only one element.
(2) basic operation of Dagen sorting algorithm:
① initialization: Constructs the R[1..N] as the initial heap;
② the basic operation of each trip: to exchange the heap top record of the current unordered area R[1] with the last record of the interval, and then adjust the new unordered area to the heap (also known as the Rebuild heap).
Attention:
① Just do n-1, choose a larger n-1 keyword that can make the file increment order.
② with small Gan is similar to the use of large root heap, except that its ordering result is descending order. Heap sorting and direct selection ordering is the opposite: at any time the unordered region in the heap sort is always before the ordered region, and the ordered region is gradually extended to the entire vector at the end of the original vector from the back forward.

Code implementation:

public class Test {public static int[] Heap = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; Preset data array public static void main (String args[]) {int i;//loop count variable int index = heap.length;//Data index variable S 
    Ystem.out.print ("Sort before:"); 
    for (i = 1; i < Index-1 i++) System.out.printf ("%3s", Heap); 
    System.out.println (""); Heapsort (Index-2); 
    Heap sort System.out.print ("after sort:"); 
    for (i = 1; i < Index-1 i++) System.out.printf ("%3s", Heap); 
  System.out.println ("");  /** * Build heap/public static void Createheap (int Root, int Index) {int I, J;//loop count variable int Temp;// Staging variable int Finish; Determine whether the heap is established to complete J = 2 * Root; The index Temp = heap[root] of the child node; The root value of the staging heap is Finish = 0; Preset heap Build not complete while (J <= index && Finish = = 0) {if (J < index)//Find the largest child node if (Heap[j] 
      < Heap[j + 1]) J + +; if (Temp >= heap[j]) Finish = 1; Heap build Complete Else { 
        HEAP[J/2] = Heap[j]; 
      Parent node = current node J = 2 * j; } HEAP[J/2] = Temp; 
    Parent node = root value} public static void Heapsort (int Index) {int I, j, Temp; 
    The two-fork tree is converted to heap for (i = (INDEX/2); I >= 1; i--) createheap (i, Index);   
      Start heap sort for (i = Index-1 i >= 1; i--) {Temp = Heap;//Heap's root value and last value exchange Heap = Heap[1];   
      HEAP[1] = Temp; Createheap (1, i);   
      For the rest of the numerical reconstruction heap System.out.print ("in Sort:");   
      for (j = 1; J <= Index; j +) System.out.printf ("%3s", heap[j));   
    System.out.println (""); }
  }
}

The heap can be seen as a tree, and the height of the node in the heap can be defined as the number of the top of the simple descent path from this node to the node of the leaf; the height of the definition heap is the height of the root. We will see that some basic operations on the heap structure run at most in direct proportion to the height of the tree, O (LGN). By reading this article, I hope I can help you.

I hope this article will help you with your Java programming.

Related Article

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.