Common search algorithm (VI): block search

Block search is also called an index sequential search, which is a sequential search for an improved method .

Algorithmic process:

  • First select each block of the largest key constitute an index table ;
  • Finding two parts: the first index table binary search or a sequential search , in order to determine the unknown origin recording in which one ; then, at block determined by sequential method to find.

 

NOTE : The idea is to n data elements "blockwise order" blocks into m (m ≤ n). Nodes in each block need not be ordered, but must be "ordered in blocks", the largest elements within each block between blocks smaller than the one of all the elements of any value .

Therefore, given a to be searched in the key , the look up the key value of the position , it will go to the index table utilized in a sequential search or a binary search to find the key block where the index start position, then according to the start block index is located location Find the key where the specific location.

Here are some block search code, which is thought as described above, it is through the index table to find the key position.

First give the main table and index table :

 1     // 主表,size=30
 2     static int[] mainList = new int[]{
 3             101, 102, 103, 104, 105, 0, 0, 0, 0, 0,
 4             201, 202, 203, 204, 0, 0, 0, 0, 0, 0,
 5             301, 302, 303, 0, 0, 0, 0, 0, 0, 0
 6     };
 7 
 8     // 索引表
 9     static IndexItem[] indexItemList = new IndexItem[]{
10             new IndexItem(1, 0, 5),
11             new IndexItem(2, 10, 4),
12             new IndexItem(3, 20, 3)
13     };

Index table categories:

    static  class indexitem {
         public  int index; // index value comparing
         public  int Start; // start position
         public  int length; // block element length (non-empty)

         public indexitem ( int index, int Start, int length) {
             the this . = index index;
             the this .start = Start;
             the this .length = length; 
        } 

        // ... getters and the setter 
    }

Index lookup algorithms:

. 1      public  static  int indexSearch ( int Key) {
 2          indexitem of an indexItem = null ;
 . 3  
. 4          // indexing rule 
. 5          int index = Key / 100 ;
 . 6  
. 7          // traverse the index table 
. 8          for ( int I = 0; I <indexItemList. length; I ++ ) {
 . 9              // find the index entry 
10              IF (indexItemList [I] == .index index) {
 . 11                  of an indexItem = indexItemList [I];
 12 is                  BREAK ;
 13 is             }
 14          }
 15  
16          // the index entry in the index table does not exist 
. 17          IF (of an indexItem == null )
 18 is              return -1 ;
 . 19  
20 is          // the index entry in the primary lookup table 
21 is          for ( int I = indexItem.start ; I <indexItem.start + indexItem.length; I ++ ) {
 22 is              IF (mainList [I] == Key)
 23 is                  return I;
 24          }
 25  
26 is          return -1 ;
 27      }

Time Complexity Analysis: press binary search to find the key index table of approximate location (given the code sequential search), then in the primary table are possible where the block position in order to find the start, the time complexity is O (log₂ (m) + N / m) the number of table elements, m is the number of sub-blocks, N based, N / m is the number of elements within each block.

Block search insertion algorithm:

 1     /**
 2      * 插入数据
 3      *
 4      * @param key 要插入的值
 5      * @return true表示插入成功,false表示插入失败
 6      */
 7     public static boolean insert(int key) {
 8         IndexItem item = null;
 9 
10         // 建立索引规则
11         int index = key / 100;
12         int i = 0;
13         // 遍历索引表,找到对应的索引项
14         for (i = 0; i < indexItemList.length; i++) {
15             if (indexItemList[i].index == index) {
16                 item = indexItemList[i];
17                 break;
18             }
19         }
20         // 索引表中不存在该索引项
21         if (item == null) {
22             return false;
23         }
24 
25         // 根据索引项将值插入到主表中
26         mainList[item.start + item.length] = key;
27         // 更新索引表
28         indexItemList[i].length++;
29 
30         return true;
31     }

打印主表的函数:

 1     /**
 2      * 遍历打印
 3      */
 4     public static void display(int[] list) {
 5         System.out.println("******** 展示开始 ********");
 6         if (list != null && list.length > 0) {
 7             for (int i = 0; i < list.length; i++) {
 8                 System.out.print(list[i] + " ");
 9                 if ((i + 1) % 10 == 0) {
10                     System.out.println("");
11                 }
12             }
13         }
14         System.out.println("******** 展示结束 ********");
15     }

测试代码:

 1     public static void main(String[] args) {
 2         System.out.println("******** 索引查找 ********");
 3         System.out.println("");
 4         System.out.println("原始数据:");
 5         display(mainList);
 6         System.out.println("");
 7 
 8         //分块查找
 9         int key = 203;
10         System.out.println("元素" + key + "列表中的位置为:" + indexSearch(key) + "\n");
11 
12         //按规则插入数据并查找
13         int value = 106;
14         System.out.println("插入数据:" + value);
15         
16         // 插入成功,查找插入位置
17         if (insert(value)) {
18             System.out.println("插入后的主表:");
19             display(mainList);
20             System.out.println("");
21 
22             System.out.println("元素" + value + "在列表中的位置为:" + indexSearch(value));
23         }
24     }

 

Guess you like

Origin www.cnblogs.com/magic-sea/p/11391431.html