首页 > 其他 > 详细

四、顺序查找

时间:2015-03-10 11:49:23      阅读:328      评论:0      收藏:0      [点我收藏+]

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

  顺序查找速度慢。

#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;
}

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

四、顺序查找

原文:http://www.cnblogs.com/gongyan/p/4325429.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!