Bentley壓麿議广恬ゞWriting Correct Programs〃嶄亟祇?90%議柴麻字廨社音嬬壓2弌扮坪亟竃頼畠屎鳩議屈蛍朴沫麻隈。
厘徭失晦編阻匯和,鳩糞勣及匯肝祥頼畠亟屎鳩音否叟.參和曾芸糞?卆肝葎亨旗才弓拷井云議旗鷹,屈蛍臥孀議房?載謹繁脅賠萱,徽頁宸戦嗤匯倖聾准祥頁勣廣吭円順議僉夲.
int search(int array[],int n,int v)
{
int left,right,middle;
left = 0,right = n - 1;
while (left <= right)
{
middle = (left + right) / 2;
if (array[middle] > v)
{
right = middle - 1;
}
else if (array[middle] < v)
{
left = middle + 1;
}
else
{
return middle;
}
}
return -1;
}
int search_recurse(int array[],int low,int high,int v)
{
int middle;
middle = (low + high) / 2;
if (low < high)
{
if (array[middle] > v)
{
return search_recurse(array,low,middle,v);
}
else if (array[middle] < v)
{
return search_recurse(array,middle + 1,high,v);
}
else
{
return middle;
}
}
else if (low == high)
{
if (array[middle] == v)
{
return middle;
}
else
{
return -1;
}
}
else
{
return -1;
}
return -1;
}
int main()
{
int array[] = {0,1,2,3,4,5,6,7,13,19};
int m = search(array,sizeof(array)/sizeof(array[0]),13);
printf("m = %d\n",m);
m = search_recurse(array,0,sizeof(array)/sizeof(array[0]),13);
printf("m = %d\n",m);
return 0;
}
|