索引幾種演算法
阿新 • • 發佈:2019-01-12
資料庫mysql索引:使用B樹索引
下面介紹一下B樹索引:
Lucene全文檢索使用:二分查詢
下面介紹一下二分查詢:
二分查詢也稱折半查詢(Binary Search),它是一種效率較高的查詢方法。但是,折半查詢要求線性表必須採用順序儲存結構,而且表中元素按關鍵字有序排列;
1、時間複雜度:因為每次的查詢都會比上一次少一半的範圍,時間複雜度為O(log n).
2、二分法必須事先經過排序,且資料量必須直接在記憶體中執行
3、此法用於不需要增刪的靜態資料
簡單舉例
public class TestTwoSearch { public static void main(String[] args) { test1(4); } public static void test1(int qNum){ int l = 0; int r = 100; int mid = 0; int idx = 0; while (l < r) { mid = (l + r) / 2; if (qNum < mid) { r = mid; } else if (qNum == mid) { System.out.println("目標" + qNum + "已經找到,查找了"+idx+"次"); break; } else { l = mid; } System.out.println("左邊:" + l + "--中間:"+mid+"--右邊:"+r); idx++; } // } } }