[題目分析]把待查記錄看作樞軸先由後向前依次比較若小於樞軸則從前向後直到查找成功返回其位置或失敗返回為止
int index (RecType R[]int lhdatatype key)
{ int i=lj=h;
while (i<j)
{ while (i<=j && R[j]key>key) j;
if (R[j]key==key) return j;
while (i<=j && R[i]key<key) i++;
if (R[i]key==key) return i;
}
printf(Not find) ; return ;
}//index
() [題目分析]從第n個記錄開始依次與其雙親(n/)比較若大於雙親則交換繼而與其雙親的雙親比較以此類推直到根為止
void sift(RecType R[]int n)
{ //假設 R[n]是大堆本算法把R[n]調成大堆
j=n; R[]=R[j];
for (i=n/;i>=;i=i/)
if (R[]key>R[i]key){ R[j]=R[i];j=i;} else break;
R[j]=R[];
}//sift
()void HeapBuilder(RecType R[]int n)
{ for (i=;i<=n;i++) sift (Ri); }
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23178.html