综合

img williamVII

二分查找算法

发表于2004/9/14 13:25:00  1347人阅读

#include "stdio.h"

int BinarySearch(const int A[], int key, int N)

{

int Low, Mid, High;

Low = 0;

High = N - 1;

while(Low <= High)

{

Mid = (Low + High) / 2;

if(A[Mid] < key)

Low = Mid + 1;

else if(A[Mid] > key)

High = Mid - 1;

else

return Mid;

}

return -1;
}

//测试:

int main()
{

int a[] = {-2, -4, 0, 5, 11, 13};

printf("BinarySearch: %d/n", BinarySearch(a, 13, size));

return 0;

}

阅读全文
0 0

相关文章推荐

img
取 消
img