//2叉堆排序
int dpx(int[] a,int i,bool isdtl,int ix)
{
ix++;
try
{
int f = i / 2;
int l = i * 2;
int r = i * 2 + 1;
int largest = i;
if (l <= a.Length)
{
if (a[l - 1] > a[i - 1])
{
largest = l;
//第一次交換
var tmp = a[i - 1];
a[i - 1] = a[l - 1];
a[l - 1] = tmp;
}
else
{
largest = i;
}
}
if (r <= a.Length)
{
if (a[r - 1] > a[i - 1])
{
largest = r;
//第二次交換
var tmp = a[i - 1];
a[i - 1] = a[r - 1];
a[r - 1] = tmp;
//var tmp = a[i - 1];
//a[i - 1] = a[largest - 1];
//a[largest - 1] = tmp;
}
}
try
{
if (a[r - 1] > a[l - 1])
{
//第三次交換
var tmp = a[r - 1];
a[r - 1] = a[l - 1];
a[l - 1] = tmp;
}
}
catch (Exception)
{}
if (largest!=i && f>0)
{
ix = dpx(a, f, false, ix);
}
else
{
if (l<=a.Length)
{
ix = dpx(a, l, true, ix);
}
if (r<=a.Length)
{
ix = dpx(a, r, true, ix);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return ix;
}