1.线性搜索:
向线性搜索中引入标记,可以将算法效率提高数倍。
//
// Created by 叶子 on 2018/1/21.
//
#include "stdio.h"
int search(int A[],int n ,int key){
int i = 0 ;
A[n] = key;
while ( A[i] != key) i ++;
return i != n;
}
int main(){
int i,n,A[10000+1],q,key,sum = 0;
scanf("%d",&n);
for( i = 0 ; i < n ; i ++) scanf("%d",&A[i]);
scanf("%d",&q);
for ( i = 0 ; i < q ; i ++){
scanf("%d",&key);
if ( search(A,n,key)) sum ++;
}
printf("%d\n",sum);
return 0;
}
2.二分搜索:将数列分为左右两部分以查得需要的值
//
// Created by 叶子 on 2018/1/21.
//
#include <stdio.h>
int A[1000000],nums=0;
int binarySearch(int key){
int left = 0 ;
int right = nums ;
int mid;
while(left<right){
mid = ( left + right ) / 2;
if ( key == A[mid] ) return 1;
if ( key > A[mid]) left = mid + 1;
else if ( key < A[mid] ) right = mid;
}
return 0;
}
int main(){
int i,q,k,sum = 0;
scanf("%d",&nums);
for ( i = 0 ; i < nums ; i ++){
scanf("%d",&A[i]);
}
scanf("%d",&q);
for ( i = 0 ; i < q ; i ++){
scanf("%d",&k);
if ( binarySearch(k)) sum ++;
}
printf("%d\n",sum);
}
3.散列法:根据元素的值确定存储位置,然后将位置保存在散列表中以实现高速搜索。
//
// Created by 叶子 on 2018/1/24.
//
//散列法
#include "stdio.h"
#include "string.h"
#define M 1046527
#define NIL (-1)
#define L 14
char H[M][L];
int getChar(char ch){
if ( ch == 'A') return 1;
else if ( ch == 'C' ) return 2;
else if ( ch == 'G' ) return 3;
else if ( ch == 'T' ) return 4;
else return 0;
}
long long getKey(char str[]){
long long sum = 0 , p = 1 ,i;
for ( i = 0 ; i < strlen(str); i ++){
sum += p*(getChar(str[i]));
p *= 5;
}
return sum;
}
int h1(int key){ return key & M;}
int h2(int key){ return 1 + ( key % ( M -1));}
int find(char str[]){
long long key ,i,h;
key = getKey(str);
for ( i = 0 ;; i ++){
h = (h1(key) + i*h2(key)) % M;
if ( strcmp(H[h],str) == 0 ) return 1;
else if ( strlen(H[h]) == 0) return 0;
}
return 0;
}
int insert(char str[]){
long long key,i,h;
key = getKey(str);
for ( i = 0 ; ; i ++){
h = (h1(key) + i* h2(key)) % M;
if (strcmp( H[h],str) == 0 ) return 1;
else if ( strlen(H[h]) == 0){
strcpy(H[h],str);
return 0;
}
}
return 0;
}
int main(){
int i,n,h;
char str[L],com[9];
for ( i = 0 ; i < M ; i ++ ) H[i][0] = '\0';
scanf("%d",&n);
for(i = 0 ; i < n ; i ++){
scanf("%s %s",com,str);
if ( com[0] == 'i'){
insert(str);
}else{
if ( find(str)){
printf("yes\n");
}else{
printf("no\n");
}
}
}
return 0;
}
4.用标准库搜索:
a.使用迭代器:
//
// Created by 叶子 on 2018/1/24.
//
//使用迭代器
#include "iostream"
#include "vector"
using namespace std;
void print(vector<int> v){
vector<int>::iterator it;
for ( it = v.begin() ; it != v.end() ; it ++){
cout << *it;
}
cout << endl;
}
int main104(){
int N = 4;
vector<int> v;
for ( int i = 0 ; i < N ; i ++){
int x;
cin >> x;
v.push_back(x);
}
print(v);
vector<int>::iterator it = v.begin();
*it = 3;
it++;
(*it)++;
print(v);
return 0;
}
b.lower_bound:
//
// Created by 叶子 on 2018/1/24.
//
#include "iostream"
#include "algorithm"
using namespace std;
int main105(){
int A[14] = { 1,1,2,2,2,4,5,5,6,8,8,8,10,15};
int *pos;
int idx;
pos = lower_bound(A,A+14,3);
idx = distance(A,pos);
cout << "A[" << idx << "] = "<<*pos<<endl;
pos = lower_bound(A,A+14,2);
idx = distance(A,pos);
cout << "A[" << idx << "] = "<< *pos << endl;
return 0;
}