常用查詢演算法 (轉)
//search.h包含了所有的常用查詢演算法
//使用順序查詢法的查詢
//seqSearch(const int arr[],int first,int last,int target)
template
int seqSearch(const T arr[],int first,int last,const T& target)
{
int i=first;
//掃描下標範圍first<=i
while (!(i==last)&&!(arr[i]==target))
i++;
return i; //i是匹配值的下標,或者,如果沒有匹配,則i=last
}
//模板函式find_last_of()的實現
template
int find_last_of(const T arr[],int first,int last,const T& target)
{
int i=last-1;
//描掃下標範圍first<=i
while(i>=first&&target!=arr[i])
i--;
if (i
}
//二分查詢演算法函式binSearch()的實現
template
int binSearch(const T arr[],int first,int last,const T& target)
{
int mid; //中間點的下標
T midValue; //用於儲存arr[mid]元素值
int origLast=last; //儲存last的初始值
while(first
mid=(first+last)/2;
midValue=arr[mid];
if (target==midValue) //有一個匹配
return mid;
//確定要查詢哪個子表
else if(target
else
first=mid+1; //查詢子表的後半部分,重新設定first
}
return origLast; //沒有找到目標值
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-963768/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle常用查詢Oracle
- 查詢演算法__Fibonacci查詢演算法
- 查詢演算法__插值查詢演算法
- Gremlin -- 常用查詢用法REM
- sql常用查詢命令SQL
- 查詢演算法__二分查詢演算法
- 查詢演算法演算法
- 演算法 - 查詢演算法
- MongoDB 常用查詢語法MongoDB
- oracle常用維護查詢Oracle
- 查詢演算法之二分查詢演算法
- Oracle 查詢轉換Oracle
- 查詢演算法(上)演算法
- 查詢演算法(下)演算法
- Java 查詢演算法Java演算法
- MogDB openGauss常用查詢彙總
- ACM常用STL查詢手冊ACM
- 二分查詢【折半查詢】演算法 PHP 版演算法PHP
- 雜湊查詢演算法演算法
- 插值查詢演算法演算法
- 子字串查詢演算法字串演算法
- MySQL 查詢常用操作(0) —— 查詢語句的執行順序MySql
- 分塊查詢【大規模資料查詢演算法優化】【索引順序查詢】演算法 PHP 版演算法優化索引PHP
- mysql dba常用的查詢語句MySql
- postgresql dba常用sql查詢語句SQL
- MongoRepository查詢資料常用語法Go
- iOS標準庫中常用資料結構和演算法之查詢iOS資料結構演算法
- Django框架:8、聚合查詢、分組查詢、F與Q查詢、ORM查詢最佳化、ORM事務操作、ORM常用欄位型別、ORM常用欄位引數Django框架ORM型別
- 深度 | 如何玩轉PG查詢處理與執行器演算法演算法
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- Oracle 查詢轉換-01 or expansionOracle
- 帶你玩玩轉 MySQL 查詢MySql
- 演算法->二分查詢演算法
- 【演算法】二分查詢演算法
- php查詢演算法的理解PHP演算法
- 如何找東西?查詢演算法之順序查詢和二分查詢詳解演算法
- 路徑查詢演算法應用之A*演算法演算法
- Oracle 查詢轉換-02 View MergingOracleView
- Oracle 查詢轉換-03 Predicate PushingOracle