Algorithm sorting-sorting Part 4 heap sorting

Source: Internet
Author: User

Heap sorting statement

The head will hurt, but there will always be a moment to stop, grasp the moment to sum up. Otherwise, it hurts.
Introduction
Heap sorting algorithms are more practical in sorting large amounts of data. Now there are several sorting algorithms. What are their advantages and disadvantages.
Ideas
The heap sorting algorithm is two-step heap creation and maintenance. Build the heap once, maintain the heap N-2 times (the first time the heap size is n-1, the last time the heap size is 2 ). For example, I like graphs. I believe you also like graphs. A total of 10 data records, as shown below:
The following data Changes During Heap creation:




The Heap has been created. Exchange data, build and reduce the heap scale, and maintain the heap until the heap scale is 1. The process is as follows for the first time.
Second
Third time
Fourth
Fifth
Sixth
Seventh
Eighth time
Final
Lab
Code
Test. cpp
// Choice sort # include
   
    
# Include
    
     
# Include
     
      
Using namespace std; // use 1000000, 00 to test int const size =; // template function declaretemplate
      
       
Void Heap_sort (T * data, const int length); // template function template
       
        
Void Make_heap (T * data, const int length); // template function template
        
          Void Heap_downcast (T * data, int I, const int length); int main () {DWORD S, E; int * data = new int [size]; for (int I = 0; I
         
           Void Heap_sort (T * data, const int length) {if (data! = NULL & length> = 0) {Make_heap (data, length); T d; int L = length; while (L> 1) {d = data [L-1]; data [L-1] = data [0]; data [0] = d; L --; Heap_downcast (data, 0, L);} cout <
          
            Void Make_heap (T * data, const int length) {if (data! = NULL & length> = 0) {for (int I = length/2-1; I> = 0; I --) {Heap_downcast (data, I, length) ;}} else {cout <"exception of input make heap" <
           
             Void Heap_downcast (T * data, int I, const int length) {if (data! = NULL & length> = 0) {T max; // have two childrenwhile (I * 2 + 2
            
              = Data [I * 2 + 1] & max> = data [2 * I + 2]) break; // right child bigger if (data [I * 2 + 2]> data [2 * I + 1] & data [I * 2 + 2]> max) {max = data [I * 2 + 2]; data [I * 2 + 2] = data [I]; data [I] = max; I = I * 2 + 2 ;} // left child biggerelse if (data [I * 2 + 1]> = data [2 * I + 2] & data [I * 2 + 1]> max) {max = data [I * 2 + 1]; data [I * 2 + 1] = data [I]; data [I] = max; I = I * 2 + 1; }}// have one childif (I * 2 + 1 <length) {if (data [I * 2 + 1]> data [I]) {max = data [I * 2 + 1]; data [I * 2 + 1] = data [I]; data [I] = max ;}} else {cout <"exception of input Heap_downcast" <
             
              

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.