查找方式:顺序查找和折半查找(二分查找);如果数据已排序,可使用折半查找和顺序查找;如果数据未排序,使用顺序查找。

  顺序查找速度慢。

#include <iostream>

using namespace std;

int SequentialSearch(int List[], const int size, const int value);

int main()
{
    int a[] = {9,3,4,5,7,8,0,1,2,3};
    
    int rtn =SequentialSearch(a, 10, 3);
    cout << "位置下标:" << rtn << endl;

    system("pause");
    return 0;
}

int SequentialSearch(int List[], const int size, const int value)
{
    int i;
    for( i = 0; i < size; i++)
    {
        if(List[i] == value)
            return i;
    }

    if(i = size)
        return -1;
}

上例只能查找第一次出现的位置。

posted on 2015-03-10 11:10  箭已离弓  阅读(110)  评论(0编辑  收藏  举报