Heapsort C language

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/hnujunjie/article/details/95538628

#include<stdio.h>
int main()
{
void swap(int * eleA,int * eleB);
void AdjustSort(int array[],int low,int high);
void HeapSort(int array[],int length);
int length;
scanf("%d",&length);
fflush(stdin);
int array [length];
for(int i = 0;i<length;i++)
{
scanf("%d",&array[i]);
}
HeapSort(array,length);
return 0;
}
void swap(int * eleA,int * eleB)
{
int temp;
temp = *eleA;
*eleA = eleB;
eleB = temp;
}
void AdjustSort(int array[],int low,int high)
{
//构建大顶堆
for(int f = low,i = 2
Low +. 1; I <= High; I = 2
I +. 1)
{
IF (I <High && Array [I] <Array [I +. 1])
{
I ++;
}
IF (Array [F]> Array [I])
{
BREAK;
}
the else
{
the swap (& Array [F], & Array [I]);
}
F = I;
}
}
void HeapSort (int Array [], int length)
{
for (int I = (length-2) / 2; I> = 0; I-)
{
// Construction accordance with the order of the respective sub-tree large stack top
AdjustSort (Array, I,. 1-length);
}
for (int. 1-I = length; I> = 0; I- )
{
// first element and the last element interchange
the swap (& Array [0], & Array [I]);
// output stack top element (maximum)
the printf ( "% D", Array [I]);
// re heap adjusted large top
AdjustSort (array, 0,i-1);
}
}

Guess you like

Origin blog.csdn.net/hnujunjie/article/details/95538628