二進位搜索(二進位查找)是一個非常快的搜索演算法。這種搜索演算法適用於分裂和治之的原則。對於該演算法正確工作數據收集應是有序的形式。
二進位搜索通過比較集合的中部專案來搜索的特定專案。如果出現匹配,那麼返回專案的索引。如果中間項大於專案,然後專案是在搜索子數組中間專案的右側的專案,否則其他會在位於子數組中間項左側搜索。 這個過程一直持續在子數組中操作直到子數組的大小減少到零。
二進位搜索減半搜索的專案,從而降低比較數量必須作出非常少的數量。
演算法
Binary Search ( A: array of item, n: total no. of items ,x: item to be searched) Step 1: Set lowerBound = 1 Step 2: Set upperBound = n Step 3: if upperBound < lowerBound go to step 12 Step 4: set midzaixian = ( lowerBound + upperBound ) / 2 Step 5: if A[midzaixian] < x Step 6: set lowerBound = midzaixian + 1 Step 7: if A[midzaixian] > x Step 8: set upperBound = midzaixian - 1 Step 9 if A[midzaixian] = x go to step 11 Step 10: Go to Step 3 Step 11: Print Element x Found at index midzaixian and go to step 13 Step 12: Print element not found Step 13: Exit
要查看二進位搜索使用數組實現在C語言編程,請點擊這裏
上一篇:
線性搜索實例程式(C語言)
下一篇:
二進位搜索/查找程式(C語言)