static void Heap_Sort(int[] a) //堆排序
{
int max=a.Length;
Heap_Make(a,max);
for (int i = max – 1; i > 0; i–)
{
swap(a, 0, i);
Heap_Del(a, 0, i);
}
Console.Write(“堆排序结果:”);
outprint(a);
}
static void Heap_Insert(int[] a, int i) //堆的插入
{
int parent=(i-1)/2;
int temp = a[i];
while(parent >= 0&&i!=0)
{
if (a[parent] <= a[i])
break;
a[i] = a[parent];
i = parent;
parent = (i – 1) / 2;
}
a[i] = temp;
}
static void Heap_Del(int[] a, int i, int max) //堆下沉操作,max是数组长度,i是调整序号
{
int child = 2 * i + 1;
int temp = a[i];
while(child<max)
{
if (child + 1 < max && a[child + 1] < a[child])
child++;
if (a[child] >= temp)
break;
a[i] = a[child];
i = child;
child = 2 * i + 1;
}
a[i] = temp;
}
static void Heap_Make(int[] a ,int max) //堆化数组
{
for (int i = (max-1) / 2 ; i >= 0; i–)
{
Heap_Del(a, i, max);
}
Console.Write(“堆化数组结果:”);
outprint(a);
}
static void Main(String[] args)
{
int[] b = { 0, -1, 5, -8, 58, 0, 98 };
int[] a = { 9, 12, 17, 30, 50, 20, 60, 65, 4, 49 } ;
int[] c={0,12,54,58,62,456,578};
Heap_Sort(a);
Console.Read();
}
}