# Include <stdio. h>
# Include <conio. h>
Void HeapAdjust (int a [], int s, int n) // large top heap
{
Int temp = a [s];
For (int j = 2 * s; j <= n; j * = 2)
{
If (j <n & a [j] <a [j + 1]) j ++;
If (temp> a [j]) break; // This error has plagued me for a long time. Temp is written as a [s]. If you forget that s [s] will change.
A [s] = a [j]; // reversed at first
S = j;
}
A [s] = temp;
}
Void HeapSort (int a [], int len)
{
For (int I = len/2; I> 0; -- I)
{
HeapAdjust (a, I, len );
} // Create an initial heap
For (int I = len; I> 1; -- I)
{
Int temp = a [1];
A [1] = a [I];
A [I] = temp;
HeapAdjust (a, 1, I-1 );
}
}
Int main ()
{
Int a [11] = {, 0 };
Printf ("Initial Sequence ");
For (int I = 1; I <11; I ++)
{
Printf ("% d", a [I]);
}
HeapSort (a, 10 );
Printf ("sorted sequence ");
For (int I = 1; I <11; I ++)
{
Printf ("% d", a [I]);
}
Getch ();
Return 0;
}
From: coder blog that does not like to stay up late