数据结构堆排序

 1 #include 
 2 typedef int KeyT;
 3 typedef int ElemType;
 4 typedef struct
 5 {
 6     KeyT key;
 7     ElemType data;
 8 }ST;
 9 void Sift(ST R[],int low,int high)
10 {
11     int i=low,j=2*i;
12     ST t=R[i];
13     while(j<=high)
14     {
15         if(j1].key)
16         j++;
17         if(t.key<R[j].key)
18         {
19             R[i]=R[j];
20             i=j;
21             j=2*i;
22         }
23         else break;
24         
25     }
26     R[i]=t;
27 }
28 void HS(ST R[],int n)
29 {
30     int i; 
31         ST t;
32         for(i=n/2;i>=1;i--)
33         Sift(R,i,n);
34         for(i=n;i>=2;i--)
35         {
36             t=R[1];
37             R[1]=R[i];R[i]=t;
38             Sift(R,1,i-1);
39         }
40 }
41 int main()
42 {
43     ST R[100];
44     KeyT A[]={75,87,68,92,88,61,77,96,80,72};
45     int i,n=10;
46     for(i=0;i)
47     R[i+1].key=A[i];
48     HS(R,n);
49     printf("排序结果:");
50     for(i=1;i<=n;i++)
51     printf("%3d",R[i].key);
52     printf("\n");
53 }

 

转载于:https://www.cnblogs.com/sxcxfl/p/11002586.html

你可能感兴趣的:(数据结构堆排序)