要求实现堆排序。请填写横线处代码,完善筛选法heapSift函数和堆排序heapSort函数,并把整段代码复制黏贴到代码提交区。#include<stdio.h> #include<stdlib.h> #define MaxSize 100 typedef int ElementType; void heapSift(ElementType R[],int i, int N) { ElementType Tmp; int Child; Tmp=R[i]; Child=2*i; while(Child <= N) { if(Child<N && R[Child]<R[Child+1]) Child++; if(Tmp>R[Child]) break; (1)________________________; (2)________________________; (3)________________________; } R[i]=Tmp; } void heapSort(ElementType R[], int N) { int i; ElementType x; for(i=N/2; i>=1; i--) (4)___________________________; for(i=N;i>1; i--) { x=R[i]; R[i]=R[1]; R[1]=x; (5)___________________________; } } int main() { int N; int i; ElementType A[MaxSize]; //请输入N的值 scanf("%d",&N); //请输入要排序的数据 for(i=1;i<=N;i++) scanf("%d",&A[i]); heapSort(A,N); // 排序结果为: for(i=1; i<=N; i++) printf("%d ", A[i]); }
时间: 2024-01-18 19:02:41 浏览: 64
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef int ElementType;
void heapSift(ElementType R[], int i, int N)
{
ElementType Tmp;
int Child;
Tmp = R[i];
Child = 2 * i;
while (Child <= N)
{
if (Child < N && R[Child] < R[Child + 1])
Child++;
if (Tmp > R[Child])
break;
R[i] = R[Child];
i = Child;
Child = 2 * i;
}
R[i] = Tmp;
}
void heapSort(ElementType R[], int N)
{
int i;
ElementType x;
for (i = N / 2; i >= 1; i--)
heapSift(R, i, N);
for (i = N; i > 1; i--)
{
x = R[i];
R[i] = R[1];
R[1] = x;
heapSift(R, 1, i - 1);
}
}
int main()
{
int N;
int i;
ElementType A[MaxSize];
//请输入N的值
scanf("%d", &N);
//请输入要排序的数据
for (i = 1; i <= N; i++)
scanf("%d", &A[i]);
heapSort(A, N);
// 排序结果为:
for (i = 1; i <= N; i++)
printf("%d ", A[i]);
}
阅读全文