标签:style io ar os sp for strong on bs
//这里说的是指针在算法中的应用}
//查找,其中涉及指针的偏移
//查找分为顺序查找
//折半查找
//儿茶查找
//分块查找
//这里举的例子是二分查找
#include <iostream>
#include <cstdio>
using namespace std;
int BinarySearch(int * Array, int n, int x)
{
int low, high, middle;
low = 0, high = n - 1;
while(low <= high)
{
middle = (low + high)/2;
if(*(Array + middle) == x)
return 1;
else
{
if(*(Array + middle) >= x)
{
high = middle - 1;
}
if(*(Array + middle) <=x)
{
low = middle + 1;
}
}
}
}
int main()
{
int Array[10] = {2, 4, 5, 13, 15, 20, 30, 35, 40, 50};
int x1, x2;
x1 = 20;
x2 = 33;
if(BinarySearch(Array, 10, x1))
printf("已经找到%d\n", x1);
else
printf("未找到%d\n", x1);
if(BinarySearch(Array, 10, x2))
printf("已经找到%d\n", x2);
else
printf("未找到%d\n", x2);
return 0;
}
标签:style io ar os sp for strong on bs
原文地址:http://blog.csdn.net/u012965373/article/details/41935515