"Algorithm design-heap sort" big root heap sort

Source: Internet
Author: User

1. Heap sorting not only has the same time complexity as the merge sort O (NLGN), but also the space complexity occupies an extra number of elements space, this has the advantage of inserting sort.

2. Heap sorting is divided into 3 parts, the first part is the function of preserving the nature of the heap max_heapify function, to maintain the nature of the heap: the parent node value is greater than the child node.

The second part is the function of creating Dagen, which build_max_heap the function from the first non-leaf node max_heapify function. Eventually create a large heap.

The third part is the heap sort section, which outputs a maximum value of key[1], and then replaces the last value with the first value, repeating.

The code is as follows:

#include <iostream>
using namespace Std;
typedef struct HEAP
{
int heap_size;
int *key;
}heap;
void Initialize (heap *a)
{
a->key=new int;
a->heap_size=0;
}
void Max_heapify (Heap *a,int i)//in order to keep the heap Dagen in nature (I use the method of cyclic operation here, and the approach of recursive invocation is applied in the introduction of algorithm)
{
Non-recursive method
int largest;
int l=2*i;//l is A's left child
int R=2*I+1;//R is A's right child
if (L<=a->heap_size&&a->key[l]>a->key[i])
Largest=l;
Else
largest=i;
if (R<=a->heap_size&&a->key[r]>a->key[largest])
Largest=r;
while (largest!=i)
{
int temp=a->key[i];
a->key[i]=a->key[largest];
a->key[largest]=temp;
I=largest;
L=2*i;
r=2*i+1;
if (L<=a->heap_size&&a->key[l]>a->key[i])
Largest=l;
Else
largest=i;
if (R<=a->heap_size&&a->key[r]>a->key[largest])
Largest=r;
}
This is a recursive method.
/*
int largest;
int l=2*i;//l is A's left child
int R=2*I+1;//R is A's right child
if (L<=a->heap_size&&a->key[l]>a->key[i])
Largest=l;
Else
largest=i;
if (R<=a->heap_size&&a->key[r]>a->key[largest])
Largest=r;
if (largest!=i)
{
int temp=a->key[i];
a->key[i]=a->key[largest];
a->key[largest]=temp;
Max_heapify (a,largest);
}*/
}
void Build_max_heap (HEAP *a)
{
int m= (a->heap_size)/2;
while (m>=1)
{
Max_heapify (A,M);
m--;
}
}
void Heap_sort (HEAP *a)
{
Build_max_heap (A);
int length=a->heap_size;
printf ("The big root heap is sorted after the array is:");
printf ("%d,", a->key[1]);
for (int i=length;i>=2;i--)
{
a->key[1]=a->key[i];
a->heap_size--;
Max_heapify (a,1);//Now only the first node of the heap is wrong, and the other subtrees are large piles
printf ("%d,", a->key[1]);
}
printf ("\ n");
printf ("done\n");
}
int main (void)
{
Heap *a=new Heap;
Initialize (A);
printf ("Please enter a set of values for the heap to end with #?") \ n ");
int key;
int i=1;
while (scanf ("%d", &key) ==1)
{
a->key[i]=key;
i++;
}
a->heap_size=i-1;
Heap_sort (A);
}

Results show:


"Algorithm design-heap sort" big root 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.