Code
#region 堆排序(第0个数据不能排序)
static private void Sift(int[] r, int i, int m)
{
int j;
int temp;
temp = r[i];
j = 2 * i;
while (j <= m)
{
if (j < m && r[j] < r[j + 1])
{
j++;
}
if (temp < r[j])
{
r[i] = r[j];
i = j;
j = 2 * i;
}
else
{
break;
}
}
r[i] = temp;
}
/// <summary>
/// 堆排序
/// </summary>
/// <param name="r">数据源</param>
/// <param name="n">元素个数</param>
static public void HeapSort(int[] r, int n)
{
int temp;
int i;
for (i = n / 2; i >= 1; i--)
{
Sift(r, i, n);
}
for (i = n; i >= 2; i--)
{
temp = r[1];
r[1] = r[i];
r[i] = temp;
Sift(r, 1, i - 1);
}
}
#endregion
#region 堆排序(第0个数据不能排序)
static private void Sift(int[] r, int i, int m)
{
int j;
int temp;
temp = r[i];
j = 2 * i;
while (j <= m)
{
if (j < m && r[j] < r[j + 1])
{
j++;
}
if (temp < r[j])
{
r[i] = r[j];
i = j;
j = 2 * i;
}
else
{
break;
}
}
r[i] = temp;
}
/// <summary>
/// 堆排序
/// </summary>
/// <param name="r">数据源</param>
/// <param name="n">元素个数</param>
static public void HeapSort(int[] r, int n)
{
int temp;
int i;
for (i = n / 2; i >= 1; i--)
{
Sift(r, i, n);
}
for (i = n; i >= 2; i--)
{
temp = r[1];
r[1] = r[i];
r[i] = temp;
Sift(r, 1, i - 1);
}
}
#endregion