Algorithm: Mister into a random number, assignment to the array, the first element of the array a[0] set to Sentinel, function call array and random number of N, and then set the root node of N/2 and the child node to compare operations, if the right child exists, then select three number of the smallest number assigned to the root node, if the right child does not exist, Then just compare the left child with the root node size, always cycle to a[1], and then from a[2] start the root node and the child node to compare operations, until N/2, and finally, output A[1], output A[N] assigned to a[1]; recursively, repeat the above steps until the array is empty
Key points: Drawing a two-fork tree is easy to understand, not the structure of the data is like a two-tree storage, storage form or a row
Compiler: VS2013
Code:
#include "stdafx.h"
#include <stdlib.h>
function declaration
void heapsort (int a[],int n); Heap sort (small to large)
void Hsort (int a[], int n);//Auxiliary heap ordering
int main ()
{
int i,n,a[100];
printf ("Please enter the number of elements that need to be sorted:");
scanf_s ("%d", &n);
printf ("Randomly generated array is:");
for (i = 1; I <= n; i++)
{
A[i] = rand ()% 100 + 1;
printf ("%d", a[i]);
}
A[i] = ' + ';
printf ("\ n");
Heapsort (A,n);
}
Auxiliary heap sequencing (small to large)
void Hsort (int a[], int n)
{
int i = N/2;
while (i)
{
if (a[2 * i + 1] = = ' + ')
{
if (a[2 * I] < a[i])
{
A[0] = a[2 * I];
A[2 * I] = a[i];
A[i] = a[0];
}
}
Else
{
if (a[2 * I] < a[2 * i + 1])
{
if (A[i] > a[2 * i])
{
A[0] = a[2 * I];
A[2 * I] = a[i];
A[i] = a[0];
}
}
Else
{
if (A[i] > a[2 * i + 1])
{
A[0] = a[2 * i + 1];
a[2 * i + 1] = A[i];
A[i] = a[0];
}
}
}
i--;
}
for (i = 2; I <= n/2; i++)
{
if (a[2 * i + 1] = = ' + ')
{
if (a[2 * I] < a[i])
{
A[0] = a[2 * I];
A[2 * I] = a[i];
A[i] = a[0];
}
}
Else
{
if (a[2 * I] < a[2 * i + 1])
{
if (A[i] > a[2 * i])
{
A[0] = a[2 * I];
A[2 * I] = a[i];
A[i] = a[0];
}
}
Else
{
if (A[i] > a[2 * i + 1])
{
A[0] = a[2 * i + 1];
a[2 * i + 1] = A[i];
A[i] = a[0];
}
}
}
}
}
Results
The heap ordering of C language data structure