Event address: CSDN 21-day learning challenge
The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; one day later, one day more mediocrity.
Article Directory
1. Concept
索引查找
Also known as 分块查找
, it is a search method between sequential search and binary search. Regarding the index, do you think of 数据库
the index in the database? After the index is established, the query speed of the database can be greatly improved. Therefore, the basic idea of index lookup is: first look up the index table, you can use binary search or sequential search, and then perform sequential search in the determined block.
There are three terms in implementing indexed lookups:
主表
, is the sequence to find索引项
, the main table is generally divided into several blocks, and an index is established for each block, and this index is called an index item.索引表
, which is the collection of index items.
Algorithm flow:
The structure of the index table is as follows, and the picture is from "A Concise Course of Data Structure":
(The following analysis part comes from the blogger "A Little Mountain Pig")
- The elements in the index table are divided into two parts: keywords and addresses
- The keyword is the attribute used for sorting extracted from the main data table, and the address is the position of the corresponding element in the main data table.
- Use binary search in the index table to quickly locate the position of the keyword to be searched
- According to the association relationship, the corresponding position in the master data table is extracted.
Algorithm efficiency analysis:
-
time complexity
-
Space Complexity:
O(n)
2. Pseudocode
left = 1
right = T.length
position = -1
while left <= right
mid = (left + right) / 2
if T[mid].key == key
position = mid
break
else if T[mid] > key
right = mid - 1
else
left = mid + 1
if position != -1
return T[position].pos
else
return -1
3. Core code implementation
package iYou.neugle.search;
import java.util.ArrayList;
import java.util.List;
public class Index_search {
class IndexItem {
public int index;
public int start;
public int length;
}
public int m = 100;
public int[] table = new int[] {
101, 102, 103, 104, 105, 201, 202, 203,
204, 301, 302, 303 };
public List<IndexItem> indexTable = new ArrayList<Index_search.IndexItem>();
public static void main(String[] args) {
Index_search index = new Index_search();
// 创建索引
index.CreateIndex();
// 向索引表中插入数据
index.InsertIndex(205);
// 利用索引表查找数据
int result = index.SearchIndex(205);
System.out.println("索引位置为" + result);
}
public void CreateIndex() {
int[] type = new int[10000];
int[] typeNum = new int[10000];
for (int i = 0; i < this.table.length; i++) {
int n = this.table[i] / m - 1;
if (type[n] == 0) {
type[n] = 1;
}
typeNum[n]++;
}
int start = 0;
for (int i = 0; i < typeNum.length; i++) {
if (typeNum[i] == 0) {
break;
}
IndexItem item = new IndexItem();
item.index = i;
item.start = start;
item.length = typeNum[i];
indexTable.add(item);
start += typeNum[i];
}
}
public void InsertIndex(int key) {
int n = key / m - 1;
int index = -1;
// 更新索引表
for (int i = 0; i < this.indexTable.size(); i++) {
if (n == this.indexTable.get(i).index) {
index = this.indexTable.get(i).start
+ this.indexTable.get(i).length;
this.indexTable.get(i).length++;
break;
}
}
for (int i = n + 1; i < this.indexTable.size(); i++) {
this.indexTable.get(i).start++;
}
// 更新数组
int[] temp = new int[this.table.length + 1];
for (int i = 0; i < temp.length; i++) {
if (i < index) {
temp[i] = this.table[i];
} else if (i == index) {
temp[i] = key;
} else {
temp[i] = this.table[i - 1];
}
}
this.table = temp;
}
public int SearchIndex(int key) {
int n = key / m - 1;
int start = -1;
int length = -1;
// 找到索引位置
for (int i = 0; i < this.indexTable.size(); i++) {
if (n == this.indexTable.get(i).index) {
start = this.indexTable.get(i).start;
length = this.indexTable.get(i).length;
break;
}
}
if (start == -1) {
return -1;
}
// 从索引位置找数据
for (int i = start; i < start + length; i++) {
if (this.table[i] == key) {
return i;
}
}
return -1;
}
}