#include<stdio.h>
//插值查找-C语言实现
//mid=low+(key-a[low])/(a[high]-a[low])*(high-low)
int sertSearch(int* pSortedSeq, int nSeqLength, int nKeyData)
{
int nLow = 0, nMid, nHigh = nSeqLength - 1;
while (nLow <= nHigh)
{
nMid = nLow + (nKeyData - pSortedSeq[nLow]) / (pSortedSeq[nHigh] - pSortedSeq[nLow]);
if (nKeyData < pSortedSeq[nMid])
{
nHigh = nMid - 1;//是mid-1,因为mid已经比较过了
}
else if (nKeyData > pSortedSeq[nMid])
{
nLow = nMid + 1;
}
else
{
return nMid;
}
}
return 0;
}
int main()
{
int AryNum[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int nLocation;
int nTarget = 4;
nLocation = sertSearch(AryNum, 9, nTarget);
printf("%d\n", nLocation);
return 0;
}
插入二分查找
最新推荐文章于 2024-05-04 22:59:00 发布