Algorithm 75 Big find: Index Lookup

Source: Internet
Author: User

The previous article summarizes the two-point lookup, which summarizes the index lookup.

On the index, we can easily think of the index in the database, set up an index, it will greatly improve the query speed of the database.

Index lookup, also known as Block lookup, is a finding method between sequential lookups and binary lookups, and the basic idea of index lookups is to look up index tables first, use binary lookups or sequential lookups, and then search sequentially in a determined block.

The following three terms need to be clarified before implementing an index lookup algorithm.

(1) Main table. That is, the sequence to find.

(2) Index entries. In general, we divide the main table into blocks, and each block creates an index, which is called the index entry.

(3) Index table. That is, the collection of indexed items.

At the same time, the index entry includes the following three points.

(1) index, which is the key word for the index entry in the main table.

(2) Start, which is the position of the 1th element within a block in the primary table.

(3) Length, that is, the size of the block.

of the index Lookup

As follows:

Code implementations for index lookups

Code:

Indexitem.java

 Public classIndexitem { Public intindex;  Public intstart;  Public intlength;  PublicIndexitem (intIndexintStartintlength) {         This. index =index;  This. Start =start;  This. length =length; }     Public intGetIndex () {returnindex; }     Public voidSetindex (intindex) {         This. index =index; }     Public intGetstart () {returnstart; }     Public voidSetstart (intstart) {         This. Start =start; }     Public intGetLength () {returnlength; }     Public voidSetLength (intlength) {         This. length =length; }}

Indexsearch.java

 Public classIndexsearch {//Main Table    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    }; //Index Table    Staticindexitem[] Indexitemlist =Newindexitem[]{NewIndexitem (1, 0, 5),            NewIndexitem (2, 10, 4),            NewIndexitem (3, 20, 3)    }; /*** Index Lookup algorithm * *@paramkey given value *@returnreturns the position of the given value in the table*/     Public Static intIndexsearch (intkey) {Indexitem Item=NULL; //Establish index rules        intindex = key/100; //① traverse the Index table to find the corresponding index entry         for(inti = 0; i < indexitemlist.length; i++) {            //Index Entry found            if(Indexitemlist[i].index = =index) {Item=Indexitemlist[i];  Break; }        }        //The index entry does not exist in the index table        if(Item = =NULL) {            return-1; }        //② based on the index entry, find in the main table         for(inti = Item.Start; I < Item.Start + item.length; i++) {            if(Mainlist[i] = =key) {                returni; }        }        return-1; }    /*** Insert Data * *@paramkey to insert the value *@returntrue for Insert success, FALSE for insert failure*/     Public Static BooleanInsertintkey) {Indexitem Item=NULL; //Establish index rules        intindex = key/100; inti = 0; //traverse the Index table to find the corresponding index entry         for(i = 0; i < indexitemlist.length; i++) {            if(Indexitemlist[i].index = =index) {Item=Indexitemlist[i];  Break; }        }        //The index entry does not exist in the index table        if(Item = =NULL) {            return false; }        //inserting values into the main table based on index entriesMainlist[item.start + item.length] =key; //Update Index Tableindexitemlist[i].length++; return true; }    /*** Traverse Print*/     Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) {             for(inti = 0; i < list.length; i++) {System.out.print (List[i]+ " "); if((i + 1)% 10 = = 0) {System.out.println (""); }}} System.out.println ("******** Show ends ********"); }     Public Static voidMain (string[] args) {System.out.println ("******** Index Lookup ********"); System.out.println (""); System.out.println ("Raw Data:");        Display (mainlist); System.out.println (""); intValue = 106; System.out.println ("Insert data:" +value); //Insert Successful        if(insert (value)) {System.out.println ("Inserted Main Table:");            Display (mainlist); System.out.println (""); System.out.println (The "element" + value + "position in the list is:" +Indexsearch (value)); }    }}

Operation Result:

Welcome reprint, but please keep the original source of the article

This address: http://www.cnblogs.com/nnngu/p/8290367.html

Algorithm 75 Big find: Index Lookup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.