#include <iostream>
#include <fstream>
using namespace std;
#define TXT_NUM_COUNT 1000000
#define TARGET_COUNT 16
int ImportToArray(const char *filename,int *array) //将filename内的纯数字文本导入数组array;
{
int count=0;
ifstream fin;
fin.open(filename);
if( fin.fail() )
{
cout<<"file read fail!"<<endl;
return -1;
}
while( fin.eof() ==false )
fin>>array[count++];
fin.close();
return count;
}
//顺序查找
int SSearch(int *array, int low, int high, int target )//返回待查数据下标,-1为查找失败
{
while(low<=high)
{
if(target==array[low])
return low;
else
++low;
}
return -1;
}
//二分查找
int BSearch(int *array, int low, int high, int target ) //target为待查数据,返回待查数据的下标.-1为查找失败
{
while(low<=high)
{
int mid=(low+high)/2;
if(target==array[mid])
return mid;
else if(target<array[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
void main()
{
int *list=new int[TXT_NUM_COUNT];
int *target=new int[TARGET_COUNT];
int count_largeW=ImportToArray("largeW_bubble.txt",list); //已知有100W个数据,待查16个;
int count_tinyW=ImportToArray("tinyW.txt",target);
for(int i=0;i<TARGET_COUNT;i++)
{
if( -1 == BSearch(list,0,count_largeW-1,target[i]) )
cout<<target[i]<<endl;
}
delete [] target;
delete [] list;
测试
//int array[]={10,20,23,25,50,90,239,611};
//int len=(sizeof(array)/sizeof(int));
//int num;
//while(cin>>num,num!=-1)
//{
// cout<<SSearch(array,0,len-1,num)<<endl;
//}
}
下图中显示的数字为不在largeW_bubble.txt中的待查数据(tinyW.txt);