資料結構基礎(2) --順序查詢 ; 二分查詢
阿新 • • 發佈:2022-05-04
順序查詢
適用範圍:
沒有進行排序的資料序列
缺點:
速度非常慢, 效率為O(N)
//實現 template <typename Type> Type *sequenceSearch(Type *begin, Type *end, const Type &searchValue) throw(std::range_error) { if ((begin == end) || (begin == NULL) || (end == NULL)) throw std::range_error("pointer unavailable"); for (Type *index = begin; index < end; ++index) { if (*index == searchValue) return index; } return end; } template <typename Type> Type *sequenceSearch(Type *array, int length, const Type &searchValue) throw(std::range_error) { return sequenceSearch(array, array+length, searchValue); }
迭代二分查詢
應用範圍:
資料必須首先排序,才能應用二分查詢;效率為(logN)
演算法思想:
譬如陣列{1, 2, 3, 4, 5, 6, 7, 8, 9},查詢元素6,用二分查詢的演算法執行的話,其順序為:
1.第一步查詢中間元素,即5,由於5<6,則6必然在5之後的陣列元素中,那麼就在{6, 7, 8, 9}中查詢,
2.尋找{6, 7, 8, 9}的中位數,為7,7>6,則6應該在7左邊的陣列元素中,那麼只剩下6,即找到了。
二分查詢演算法就是不斷將陣列進行對半分割,每次拿中間元素和目標元素進行比較。
//實現:迭代二分 template <typename Type> Type *binarySearch(Type *begin, Type *end, const Type &searchValue) throw(std::range_error) { if ((begin == end) || (begin == NULL) || (end == NULL)) throw std::range_error("pointer unavailable"); /**注意:此處high為end-1,並不是end 因為在後續的查詢過程中,可能會如下操作 (*high), 或等價的操作 此時應該訪問的是最後一個元素, 必須注意不能對陣列進行越界訪問! */ Type *low = begin, *high = end-1; while (low <= high) { //計算中間元素 Type *mid = low + (high-low)/2; //如果中間元素的值==要找的數值, 則直接返回 if (*mid == searchValue) return mid; //如果要找的數比中間元素大, 則在陣列的後半部分查詢 else if (searchValue > *mid) low = mid + 1; //如果要找的數比中間元素小, 則在陣列的前半部分查詢 else high = mid - 1; } return end; } template <typename Type> Type *binarySearch(Type *array, int length, const Type &searchValue) throw(std::range_error) { return binarySearch(array, array+length, searchValue); } 遞迴簡介 遞迴就是遞迴...(自己呼叫自己),遞迴的是神,迭代的是人; 遞迴與非遞迴的比較 [cpp] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片 //遞迴求解斐波那契數列 unsigned long ficonacciRecursion(int n) { if (n == 1 || n == 2) return 1; else return ficonacciRecursion(n-1) + ficonacciRecursion(n-2); }
//非遞迴求解斐波那契數列 unsigned long ficonacciLoop(int n) { if (n == 1 || n == 2) return 1; unsigned long first = 1, second = 1; unsigned long ans = first + second; for (int i = 3; i <= n; ++i) { ans = first + second; first = second; second = ans; } return ans; }
遞迴二分查詢
演算法思想如同迭代二分查詢;
//實現
template <typename Type>
Type *binarySearchByRecursion(Type *front, Type *last, const Type &searchValue)
throw(std::range_error)
{
if ((front == NULL) || (last == NULL))
throw std::range_error("pointer unavailable");
if (front <= last)
{
Type *mid = front + (last-front)/2;
if (*mid == searchValue)
return mid;
else if (searchValue > *mid)
return binarySearchByRecursion(mid+1, last, searchValue);
else
return binarySearchByRecursion(front, mid-1, searchValue);
}
return NULL;
}
template <typename Type>
int binarySearchByRecursion(Type *array, int left, int right, const Type &searchValue)
throw (std::range_error)
{
if (array == NULL)
throw std::range_error("pointer unavailable");
if (left <= right)
{
int mid = left + (right-left)/2;
if (array[mid] == searchValue)
return mid;
else if (searchValue < array[mid])
return binarySearchByRecursion(array, left, mid-1, searchValue);
else
return binarySearchByRecursion(array, mid+1, right, searchValue);
}
return -1;
}
小結:
其實C++ 的STL已經實現好了std::binary_search(),在用的時候我們只需呼叫即可, 但是二分演算法的思想還是非常重要的, 在求解一些較為複雜的問題時, 我們時常能夠看到二分的身影.