Sorting algorithm of data structure (i)-heap sort

Source: Internet
Author: User

Following the previous article on the implementation of the heap http://blog.csdn.net/tuke_tuke/article/details/50357939, you can use the heap to implement heap sorting.

The idea of implementing heap sequencing on a heap is simple: (the smallest heap is used here, and of course the largest heap can be)

The minimum heap minheap is the location of the minimum number of root elements in the heap. first, build a group of numbers, and then constantly remove the root elements of the heap, because each removal of the root element is the smallest element of the existing heap, it can get all the elements from small to large order.

Minheap.java

Package Heapsort;import Java.util.arraylist;public class Minheap <e extends Comparable>{private ArrayList<E > List=new arraylist<e> ();//using an array to implement heap public minheap () {} public Minheap (e[] objects) {for (int i=0;i<obje    cts.length;i++) {Add (objects[i]);    }} public void Add (E newObject) {//Add an element List.add (NewObject);        int Currentindex=list.size ()-1; while (currentindex>0) {int parentindex= (currentIndex-1)/2;//Find the parent node of the node if (List.get (currentindex). CompareTo (    List.get (Parentindex)) <0) {//If the value of the current node is less than the parent node, swap position E temp=list.get (currentindex);    List.set (Currentindex, List.get (Parentindex));       List.set (Parentindex, temp);    } else break;    Currentindex=parentindex;        }} public E remove () {//delete and return the root node if (list.size () ==0) return null;    E removeobject=list.get (0);        List.set (0, List.get (list.size ()-1));//Place the last node at the root node List.remove (list.size ()-1);    int currentindex=0; while (currentindex<list. Size ()) {int leftchildindex=2*currentindex+1;    int rightchildindex=2*currentindex+2;//around the child node coordinates if (Leftchildindex>=list.size ()) break;     Compare the value of the left and right child, so that maxindex points to a small value of the node int minindex=leftchildindex; if (Rightchildindex<list.size ()) {if (List.get (Minindex). CompareTo (List.get (Rightchildindex)) >0) {minindex=     Rightchildindex;              }}//If the value of the current node is greater than the large value of its left and right children, swap two nodes if (List.get (Currentindex)). CompareTo (List.get (Minindex)) >0) {              E Temp=list.get (Minindex);              List.set (Minindex, List.get (Currentindex));              List.set (Currentindex, temp);        Currentindex=minindex;    } else break;           } return removeobject;    } public int GetSize () {return list.size (); }}
Heapsort.java

Package Heapsort;import Java.awt.list;public class Heapsort {public static<e extends comparable> void Heapsort (E[] List) {minheap<e> heap=new minheap<e> ()///Minimum heap class//Add array to heap first, build heap for (int i=0;i<list.length;i++) {Heap.add (List[i]);} Then at one time the root node is deleted, the root node is always the maximum value/*for (int i=list.length-1;i>=0;i--) {//List[i]=heap.remove () with the largest heap ordering;} */for (int i=0;i<=list.length-1;i++) {//uses the minimum heap sort to continuously remove the root element of the heap list[i]=heap.remove ();//adjusts heap}}public static void each time Main (string[] args) {//TODO auto-generated Method stub         integer[] list={22,43,11,24,27,21,54,35,23};         System.out.println ("The array before the heap sort is:");         for (int i=0;i<list.length;i++) {         System.out.print (list[i]+ "  ");         }         Heapsort (list);//Heap sort         System.out.println ();         System.out.println ("The array after which the heap is sorted is:");         for (int i=0;i<list.length;i++) {         System.out.print (list[i]+ "  ");}}}         


Algorithm Analysis :

The run time is mainly spent on the initial build heap and on repeated filtering when rebuilding the heap .
In the process of building the heap, because we are completely binary tree from the bottom right of the non-terminal node to build, compare it with their children and if necessary interchange, for each non-terminal node, in fact, two times the comparison and interchange operations, so the entire build heap time complexity is O (n).
In the formal order, the first time to fetch the heap top record to rebuild the heap needs O (logi) time (a complete binary tree node to the root node distance of log2i+1), and need to take n-1 heap top record, therefore, the time complexity of rebuilding the heap is O (Nlogn).
In general, Heap sequencing has a time complexity of O (Nlogn). Because heap sorting is not sensitive to the sequencing state of the original record, it is O (Nlogn) for both best, worst, and average time complexity.


Sorting algorithm of data structure (i)-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.