对整数数组进行二分查找;传数组指针会丢失数组大小信息。

来源:互联网 发布:信息系统网络拓扑图 编辑:程序博客网 时间:2024/05/16 16:20

二分查找是一个迭代(iterator)过程,它所搜索的对象是一个有序(这里用的升序)的数组。

思想:

1. 判断左右指针是否合理:若不合理则迭代结束;否则进行2。

2. 判断以左右指针批向的点为边界时,中间点是否与查找点相等:若相等,找到目标,结束;否则,进行3。

3. 判断左右指针与中间点的大小关系(经过2,已经判断过相等的情况):中间点大于目标,右指针移到中间点左一位置;中间点小于目标,左指针移到中间点右一位置。返回1。

代码实现:

int main (){    int a_size = 30;    int a[30] = {64, 84, 42, 4, 95, 35, 16, 33, 82, 16, 95, 28, 63, 35, 90, 11, 49, 77, 91, 2};        sort(a, a + a_size); // 给数组排一下序    int X = 16; // 待查找数    int left = 0, right = a_size - 1; // 初始化左右指针        for (int i = 0; i < a_size; i++) // 打印出排序后的数组。    {        cout<<"Index:["<<i<<"]:"<<a[i]<<endl;    }    cout<<endl;    while(1)    {        if (left > right) // 没找到目标            break;        int mid = (left + right)/2; // 折半        if(a[mid] == X)        {            cout<<"Find "<<X<<" in position: "<<mid<<"\tIndex: "<<mid - 1<<endl; // 找到目标,给出信息            return mid;        }        else if (a[mid] > X) // 中间数大于目标,左指针移到中间数右一位置        {            left = mid +1;         }        else // 中间数小于目标,右指针移到中间数左一位置            right = mid - 1;    }    cout<<X<<" not in array!"<<endl;    return 0;}


下面说一个在与二分查算法找无关,而是在实现过程中遇到的一个问题:传数组指针会丢失数组大小信息。

int findX(int *a, int X){    int a_size = (sizeof(a)/sizeof(*a));    cout<<"sizeof(a):"<<sizeof(a)<<endl; // ? sizeof(a) == 8 is true? Sizeof on array function parameter will return size of 'int *' instead of 'int []'    // 这里得到的size是指针的大小。也就是说,现在指向数组a的指针变量的大小是sizeof(a).    cout<<"sizeof(*a):"<<sizeof(*a)<<endl;    cout<<a[2]<<endl;    int left = 0, right = a_size - 1;    sort(a, a + a_size);//    cout<<*a<<endl;    cout<<"size of array:"<<a_size<<endl;    for (int i = 0; i < a_size; i++) {        cout<<a[i]<<"\t";    }    cout<<endl;    while(1)    {        if (left > right)            break;        int mid = (left + right)/2;        if(a[mid] == X)        {            cout<<" find "<<X<<" in position: "<<mid<<endl;            return mid;        }        else if (a[mid] > X)        {            left = mid +1;        }        else            right = mid - 1;    }    cout<<X<<" not in array!"<<endl;    return -1;}int main (){    int a[30] = {64, 84, 42, 4, 95, 35, 16, 33, 82, 16, 95, 28, 63, 35, 90, 11, 49, 77, 91, 2};    cout<<"sizeof(a):"<<sizeof(a)<<endl;    cout<<"sizeof(*a):"<<sizeof(*a)<<endl;    cout<<findX(a, 5)<<endl;    return 0;}

解决这个问题可以用function template:

template <typename T, int N> // 在这里相当于定义两个待赋值的变量,一个是数据类型变量T,一个是整数Nint findX(T (&a)[N], int X) //  在这里把数组int a[n]的数据类型int赋给T, 数组大小赋给N{    int a_size = N;    cout<<"sizeof(a):"<<sizeof(a)<<endl; // 现在就没有问题了。    cout<<"sizeof(*a):"<<sizeof(*a)<<endl;    cout<<a[2]<<endl;    int left = 0, right = a_size - 1;    sort(a, a + a_size);//    cout<<*a<<endl;    cout<<"size of array:"<<a_size<<endl;    for (int i = 0; i < a_size; i++) {        cout<<a[i]<<"\t";    }    cout<<endl;    while(1)    {        if (left > right)            break;        int mid = (left + right)/2;        if(a[mid] == X)        {            cout<<" find "<<X<<" in position: "<<mid<<endl;            return mid;        }        else if (a[mid] > X)        {            left = mid +1;        }        else            right = mid - 1;    }    cout<<X<<" not in array!"<<endl;    return -1;}int main (){//    test_point();//    int a[20]={2,4,1,23,5,76,0,43,24,65};    int a[30] = {64, 84, 42, 4, 95, 35, 16, 33, 82, 16, 95, 28, 63, 35, 90, 11, 49, 77, 91, 2};    cout<<"sizeof(a):"<<sizeof(a)<<endl;    cout<<"sizeof(*a):"<<sizeof(*a)<<endl;    cout<<findX(a, 5)<<endl;    cout<<"test"<<endl;    int a_size = (sizeof(a)/sizeof(*a));    return 0;}




参考: http://stackoverflow.com/questions/5724171/passing-an-array-by-reference

http://stackoverflow.com/questions/968001/determine-size-of-array-if-passed-to-function

0 0