C Language implementation of heap creation in data structures, heap sequencing

Source: Internet
Author: User

#include "stdio.h"
#include "Stdlib.h"
void swap (int *a,int *b)//Exchange two numbers
{
int t;
T=*a;
*a=*b;
*b=t;
}
void minheap (int *a,int i)//Keng Gen adjustment, in the I-node upward adjustment, based on a given array, the creation of small Gan for the arrays
{
int temp,j;
Temp=a[i];
j= (i-1)/2; The parent node of the I node
while (j>=0&&i!=0)
{
if (a[j]<temp) break;
A[I]=A[J];
I=j;
j= (i-1)/2;
}
A[i]=temp;
}
void Add (int *a,int key,int N)//insert a node at the end and make an adjustment
{
A[n]=key;
Minheap (A,n);
}
void Minheapfix (int *a,int i,int N)//adjustment after deleting node I
{
int j,temp;
Temp=a[i];
j=2*i+1;
while (J<n)
{
if (J+1<n&&a[j+1]<a[j])
j + +;
if (a[j]>temp) break;
A[I]=A[J];
I=j;
j=2*i+1;
}
A[i]=temp;
}
void Deletenumber (int a[],int n)//Delete last node
{
Swap (&a[0],&a[n-1]);
Minheapfix (a,0,n-1);
}
void sort (int a[],int n)//Sort small Gan, small Gan a[0] is the minimum value, each time and the last node is interchanged, then the heap is adjusted, and so on
{
for (int i=n-1;i>=1;i--)
{
Swap (&a[i],&a[0]);
Minheapfix (A,0,i);
}
}
int main ()
{
int a[7]={2,3,4,2,5,6,3},i;
for (i=0;i<7;i++)
Add (a,a[i],i);
Sort (a,7);
for (i=0;i<7;i++)
printf ("%d", a[i]);
return 0;
}

C Language implementation of heap creation in data structures, heap sequencing

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.