1. 程式人生 > >算法7 五大查找之:索引查找

算法7 五大查找之:索引查找

.html spa 代碼 技術分享 存在 val amp title key

上一篇總結了二分查找,這一篇要總結的是索引查找。

關於索引,我們很容易地聯想到數據庫中的索引,建立了索引,可以大大提高數據庫的查詢速度。

索引查找又稱為分塊查找,是一種介於順序查找和二分查找之間的一種查找方法,索引查找的基本思想是:首先查找索引表,可用二分查找或順序查找,然後在確定的塊中進行順序查找。

在實現索引查找算法前需要弄清楚以下三個術語。

(1)主表。即要查找的序列。

(2)索引項。一般我們會將主表分成幾個塊,每個塊建立一個索引,這個索引就叫索引項。

(3)索引表。即索引項的集合。

同時,索引項包括以下三點。

(1)index,即索引項在主表的關鍵字。

(2)start,即塊內的第1個元素在主表中的位置。

(3)length,即塊的長度。

索引查找的示意圖

示意圖如下:

技術分享圖片

索引查找的代碼實現

代碼:

IndexItem.java

public class IndexItem {
    public int index;
    public int start;
    public int length;

    public IndexItem(int index, int start, int length) {
        this.index = index;
        this.start = start;
        this
.length = length; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public int getStart() { return start; } public void setStart(int start) { this.start = start; }
public int getLength() { return length; } public void setLength(int length) { this.length = length; } }

IndexSearch.java

public class IndexSearch {
    // 主表
    static int[] mainList = new int[]{
            101, 102, 103, 104, 105, 0, 0, 0, 0, 0,
            201, 202, 203, 204, 0, 0, 0, 0, 0, 0,
            301, 302, 303, 0, 0, 0, 0, 0, 0, 0
    };

    // 索引表
    static IndexItem[] indexItemList = new IndexItem[]{
            new IndexItem(1, 0, 5),
            new IndexItem(2, 10, 4),
            new IndexItem(3, 20, 3)
    };

    /**
     * 索引查找算法
     *
     * @param key 給定值
     * @return 返回給定值在表中的位置
     */
    public static int indexSearch(int key) {
        IndexItem item = null;

        // 建立索引規則
        int index = key / 100;

        // ① 遍歷索引表,找到對應的索引項
        for (int i = 0; i < indexItemList.length; i++) {
            // 找到索引項
            if (indexItemList[i].index == index) {
                item = indexItemList[i];
                break;
            }
        }

        // 索引表中不存在該索引項
        if (item == null) {
            return -1;
        }

        // ② 根據索引項,在主表中查找
        for (int i = item.start; i < item.start + item.length; i++) {
            if (mainList[i] == key) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 插入數據
     *
     * @param key 要插入的值
     * @return true表示插入成功,false表示插入失敗
     */
    public static boolean insert(int key) {
        IndexItem item = null;

        // 建立索引規則
        int index = key / 100;
        int i = 0;
        // 遍歷索引表,找到對應的索引項
        for (i = 0; i < indexItemList.length; i++) {
            if (indexItemList[i].index == index) {
                item = indexItemList[i];
                break;
            }
        }
        // 索引表中不存在該索引項
        if (item == null) {
            return false;
        }

        // 根據索引項將值插入到主表中
        mainList[item.start + item.length] = key;
        // 更新索引表
        indexItemList[i].length++;

        return true;
    }

    /**
     * 遍歷打印
     */
    public static void display(int[] list) {
        System.out.println("********展示開始********");
        if (list != null && list.length > 0) {
            for (int i = 0; i < list.length; i++) {
                System.out.print(list[i] + " ");
                if ((i + 1) % 10 == 0) {
                    System.out.println("");
                }
            }
        }
        System.out.println("********展示結束********");
    }

    public static void main(String[] args) {
        System.out.println("********索引查找********");
        System.out.println("");
        System.out.println("原始數據:");
        display(mainList);
        System.out.println("");

        int value = 106;
        System.out.println("插入數據:" + value);
        // 插入成功
        if (insert(value)) {
            System.out.println("插入後的主表:");
            display(mainList);
            System.out.println("");

            System.out.println("元素" + value + "在列表中的位置為:" + indexSearch(value));
        }
    }
}

運行結果:

技術分享圖片

歡迎轉載,但請保留文章原始出處

本文地址:http://www.cnblogs.com/nnngu/p/8290367.html

算法7 五大查找之:索引查找