Sorting problem of data structure---heap ordering and the complexity of various sorting time space

Source: Internet
Author: User

The basic idea of heap sorting: to sort by establishing the maximum minimum heap according to a complete binary tree

Complexity of Time: O (NLONGN)

/** *  */Package com;/** * @author Wenb * @time pm 03:52:55 * @date 2014-10-24*/ Public classHeapsort { Public Static voidMain (String args[]) {int[] A = {1,3,2, +,5, A,98, Wu};          Headsort (a);  for(intI=0; i<a.length;i++) {System. out. Print (a[i]+" "); }      }           Public Static int[] Headsort (int[] sortarray) {                   for(intI=0; i<sortarray.length-1; i++) {buildmaxheap (sortarray,sortarray.length-1-i); Swap (Sortarray,0, sortarray.length-1-i); }               returnSortarray; }          //ways to exchange two data     Public Static voidSwapint[] Data,intIintj) {                  inttemp =Data[i]; Data[i]=Data[j]; DATA[J]=temp; }          //Build Maximum Heap     Public Static voidBuildmaxheap (int[] Data,intLastIndex) {                 //start the ship stack from the parent node of the lastindex node         for(intI= (lastindex-1)/2; i>=0; i--){              //Save the node you are judging            intK =i; //This creates a large top heap for each node, as long as the root node has child nodes             while((2*k+1) <=LastIndex) {                                  //assuming the value of the left node is the largest                intBiggerindex =2*k+1; //It means there's a right node.                if(Biggerindex <LastIndex) {                      //Select the largest value in the child node                    if(Data[biggerindex] < data[biggerindex+1]) {Biggerindex++; }                  }                  //compare nodes to child nodes                if(Data[k] <Data[biggerindex])                      {swap (Data,k,biggerindex); K=Biggerindex; }Else{                                          Break; }              }          }      }   }

Various sorting complexities:

Specific analysis: http://blog.sina.com.cn/s/blog_771849d301010ta0.html

Sorting problem of data structure---heap ordering and the complexity of various sorting time space

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.