- //============================================================================
- // Function:顺序搜索,见《Analysis of Algorithms - An Active Learning Approach》
- // W(N)=N
- // In ——待搜索数组 list(类型 int*);
- // 目标值 target(类型 int);
- // 待搜索数组长度 n(类型 int)。
- // Out——无。
- // return:目标在数组中的位置 i(类型 int,0为起始位置,n为未找到)。
- // note:list在搜索前后未作改动
- // bug:如果n大于list已分配长度,则发生内存溢出
- //============================================================================
- int SequentialSearch(int *list,int target,int n)
- {
- for(int i=0;i<n;i++){
- if(list[i]==target){
- return i;
- }
- }
- return n;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1607201513113.html
来源: http://www.codesnippet.cn/detail/1607201513113.html