Array Implementation of sorting algorithms-heap sorting (2)

Source: Internet
Author: User

Heap sorting:


[Cpp]
Void static Heap_ExChange (int & a, int & B)
{
Int temp =;
A = B;
B = temp;
}
 
Int Heap_Parent (int n_children)
{
Return (n_children-1)/2;
}
 
Int Heap_Left (int n_parent)
{
Return 2 * n_parent + 1;
}
 
Int Heap_Right (int n_parent)
{
Return 2 * (n_parent + 1 );
}

Void static Max_Heapify (int * a, int heap_size, int n_parent)
{
Int n_Max = 0;
Int n_L = Heap_Left (n_parent );
Int n_R = Heap_Right (n_parent );

If (n_L <= heap_size-1 & a [n_L]> a [n_parent])
N_Max = n_L;
Else
N_Max = n_parent;
 
If (n_R <= heap_size-1 & a [n_R]> a [n_Max])
N_Max = n_R;

If (n_Max! = N_parent)
{
Heap_ExChange (a [n_Max], a [n_parent]);
Max_Heapify (a, heap_size, n_Max );
}
}
 
Void static Build_Max_Heap (int * a, int size)
{
Int heap_size = size;
 
For (int I = heap_size/2-1; I> = 0; I --)
{
Max_Heapify (a, heap_size, I );
}
}
 
Void Heap_Sort (int * a, int size)
{
Int heap_size = size;
Build_Max_Heap (a, heap_size );

For (int I = size-1; I> = 1; I --)
{
Heap_ExChange (a [0], a [I]);
Heap_size --;
Max_Heapify (a, heap_size, 0 );
}
 
}

Void static Heap_ExChange (int & a, int & B)
{
Int temp =;
A = B;
B = temp;
}

Int Heap_Parent (int n_children)
{
Return (n_children-1)/2;
}

Int Heap_Left (int n_parent)
{
Return 2 * n_parent + 1;
}

Int Heap_Right (int n_parent)
{
Return 2 * (n_parent + 1 );
}
 
Void static Max_Heapify (int * a, int heap_size, int n_parent)
{
Int n_Max = 0;
Int n_L = Heap_Left (n_parent );
Int n_R = Heap_Right (n_parent );
 
If (n_L <= heap_size-1 & a [n_L]> a [n_parent])
N_Max = n_L;
Else
N_Max = n_parent;

If (n_R <= heap_size-1 & a [n_R]> a [n_Max])
N_Max = n_R;
 
If (n_Max! = N_parent)
{
Heap_ExChange (a [n_Max], a [n_parent]);
Max_Heapify (a, heap_size, n_Max );
}
}

Void static Build_Max_Heap (int * a, int size)
{
Int heap_size = size;

For (int I = heap_size/2-1; I> = 0; I --)
{
Max_Heapify (a, heap_size, I );
}
}

Void Heap_Sort (int * a, int size)
{
Int heap_size = size;
Build_Max_Heap (a, heap_size );
 
For (int I = size-1; I> = 1; I --)
{
Heap_ExChange (a [0], a [I]);
Heap_size --;
Max_Heapify (a, heap_size, 0 );
}

}

Author: wchm_seu

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.