The heap ordering of C language data structure

Source: Internet
Author: User

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

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.