void fixDown1(int a[],int k,int n)
{
int j;
while (3*k <= n)
{
j = 3*k;
if ((j +1<= n)&&(a[j] < a[k])&&(a[j-1]<a[k])&&(a[j+1]<a[k]))
{
break;
}
int max = 0;
if ((j <= n)&&(a[j] < a[j-1]))
{
max =j-1;
}
else if ((j <= n)&&(a[j] >= a[j-1]))
{
max = j;
}
if ((j+1 <= n)&&(a[j+1] > a[max]))
{
max = j+1;
}
if (a[max] > a[k])
{//childer swap parent;这儿导致排序不稳定的结果的
int temp = a[max];
a[max] = a[k];
a[k] = temp;
}
k = max;
}
if (3*k-1 <= n)
{
j = 3*k-1;
if (a[j] > a[k])
{
int temp = a[j];
a[j] = a[k];
a[k] = temp;
k = j;
}
}
}
void heapsort1(int a[],int l,int r)
{//三叉堆
int k,sum = r-l+1;
for (k = ceil(sum/3.0);k >=1; k--)
{
fixDown1(g_array, k, sum);
}
while (sum > 1)
{
int temp = g_array[1];
g_array[1] = g_array[sum];
g_array[sum] = temp;
fixDown1(g_array, 1, --sum);
}
}
{
int j;
while (3*k <= n)
{
j = 3*k;
if ((j +1<= n)&&(a[j] < a[k])&&(a[j-1]<a[k])&&(a[j+1]<a[k]))
{
break;
}
int max = 0;
if ((j <= n)&&(a[j] < a[j-1]))
{
max =j-1;
}
else if ((j <= n)&&(a[j] >= a[j-1]))
{
max = j;
}
if ((j+1 <= n)&&(a[j+1] > a[max]))
{
max = j+1;
}
if (a[max] > a[k])
{//childer swap parent;这儿导致排序不稳定的结果的
int temp = a[max];
a[max] = a[k];
a[k] = temp;
}
k = max;
}
if (3*k-1 <= n)
{
j = 3*k-1;
if (a[j] > a[k])
{
int temp = a[j];
a[j] = a[k];
a[k] = temp;
k = j;
}
}
}
void heapsort1(int a[],int l,int r)
{//三叉堆
int k,sum = r-l+1;
for (k = ceil(sum/3.0);k >=1; k--)
{
fixDown1(g_array, k, sum);
}
while (sum > 1)
{
int temp = g_array[1];
g_array[1] = g_array[sum];
g_array[sum] = temp;
fixDown1(g_array, 1, --sum);
}
}