静态表,在折半查找前,一般要对其进行排序,这样才能折半。如果是顺序查找是不要求的。
1、单向冒泡排序:静态查找表在升序排序时,用到冒泡排序。在冒泡排序时每一循环都使得最后的数据时稳定的,也就是说第一次最大的数据在最后,第二次次大的数据在倒数第二,这样在每次循环时只从j=0到j
for(int i=0;i
{
for(int j=0;j
{
if(ST[j].key>ST[j+1].key)
{
t=ST[j].key;
ST[j].key=ST[j+1].key;
ST[j+1].key=t;
}
}
}
2、折半查找:递归查找和非递归查找
2.1)递归:
template
int SSearch::BiSearch_2(T key)
{
int k;
k=BiSearch2(0,len-1,key);
return k;
}
template
int SSearch::BiSearch2(int x, int y,T key) //recursive to find the key InBtween x an y,inlcude x and y
{
int mid=(x+y)/2;
if(ST[mid].key==key)
return mid;
if(x>y)
return -1;
if(key< ST[mid].key)
return BiSearch2(x,mid-1,key);
else
return BiSearch2(mid+1,y,key);
}
2.2)非递归:用到三个标志,low,high,mid.
template
int SSearch::BiSearch_1(T key) //折半查找
{
if(len==0&&ST==NULL)
{
cout<<"The Sort Table doesn't exit, plz CREAT it first!/n";
return -1;
}
else
{
int low=0,high=len-1;
int mid;
while(low<=high)
{
mid=(high+low)/2;
if(ST[mid].key==key)
{
cout<<"Find! Locate at :"<<<"/n";
return mid;
}
if(ST[mid].key>key)
high=mid-1;
else
low=mid+1;
}
cout<<"CANNOT FIND/n";
return -1;
}
}
#ifndef _STATIC_SEARCH_H #define _STATIC_SEARCH_H //静态查找表 typedef int T; template struct Node { T key; /* */ }; template class SSearch{ private: Node * ST; int len; public: SSearch(); ~SSearch(); void Create(int n); void Display(); int SeSearch(T key); void Ascendsort(); int BiSearch_1(T key); int BiSearch_2(T key); int BiSearch2(int x,int y, T key); }; #endif
// StaticSearch.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "StaticSearch.h" #include using namespace std; template SSearch::SSearch() { ST=NULL; len=0; } template SSearch::~SSearch() { delete []ST; len=0; } template void SSearch::Create(int n) { len=n; ST=new Node[len]; Node e; int i=0; cout<<"INPUT:"<<<" DATA ELEMENT/n"; while(i { cin>>e.key; ST[i]=e; i++; } } template int SSearch::SeSearch(T key) { for(int i=len-1;i>=0;i--) if(ST[i].key==key) { cout<<"Find SUCCESS! Locate at:"<<<"/n"; return i; } cout<<"CANNOT FIND/n"; return -1; } template void SSearch::Ascendsort() { T t; for(int i=0;i { for(int j=0;j { if(ST[j].key>ST[j+1].key) { t=ST[j].key; ST[j].key=ST[j+1].key; ST[j+1].key=t; } } } cout<<"Ascend SORT SUCCESS|!/n"; } template int SSearch::BiSearch_1(T key) //折半查找 { if(len==0&&ST==NULL) { cout<<"The Sort Table doesn't exit, plz CREAT it first!/n"; return -1; } else { int low=0,high=len-1; int mid; while(low<=high) { mid=(high+low)/2; if(ST[mid].key==key) { cout<<"Find! Locate at :"<<<"/n"; return mid; } if(ST[mid].key>key) high=mid-1; else low=mid+1; } cout<<"CANNOT FIND/n"; return -1; } } template int SSearch::BiSearch2(int x, int y,T key) //recursive to find the key InBtween x an y,inlcude x and y { int mid=(x+y)/2; if(ST[mid].key==key) return mid; if(x>y) return -1; if(key< ST[mid].key) return BiSearch2(x,mid-1,key); else return BiSearch2(mid+1,y,key); } template int SSearch::BiSearch_2(T key) { int k; k=BiSearch2(0,len-1,key); return k; } template void SSearch::Display() { cout<<"Static Table keys are:/n"; for(int i=0;i { cout<<<" "; } cout<<"/n"; } int _tmain(int argc, _TCHAR* argv[]) { int choice, n,k; SSearch a; cout<<"ATTENSSION:must CREAT the table fisrt~/n"; T key; do { cout<<"----1.Create Tables--------------------/n" <<"----2.Sequential Find--------------------/n" <<"----3.Binary Search(Recursive)--------------------/n" <<"----4.Binary Search(Non Recursive)--------------------/n" <<"----5.Output the keys in Static Table--------------------/n" <<"----6.QUIT--------------------/n"; cin>>choice; switch(choice) { case 1: cout<<"Input the length of the Static Table/n"; cin>>n; a.Create(n); break; case 2: cout<<"Input The key to Search:/n"; cin>>key; a.SeSearch(key); break; case 3: a.Ascendsort(); cout<<"Input the key to Search:/n"; cin>>key; k=a.BiSearch_2(key); if(k==-1) cout<<"Didn't Find!/n"; else cout<<"Found!Locate At :"<<<"/n"; break; case 4: a.Ascendsort(); cout<<"Input the key to Search:/n"; cin>>key; a.BiSearch_1(key); break; case 5: a.Display(); break; case 6: cout<<"End/n"; break; } }while(choice!=0); return 0; }
4738

被折叠的 条评论
为什么被折叠?



