1 PackageIYou.neugle.search;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Public classIndex_search {7 classIndexitem {8 Public intindex;9 Public intstart;Ten Public intlength; One } A - Public intm = 100; - the Public int[] table =New int[] {101, 102, 103, 104, 105, 201, 202, 203, -204, 301, 302, 303 }; - - PublicList<indexitem> indextable =NewArraylist<index_search. Indexitem>(); + - Public Static voidMain (string[] args) { +Index_search index =NewIndex_search (); A //Create an index at index. CreateIndex (); - //inserting data into the index table -Index. Insertindex (205); - //finding data using an index table - intresult = index. Searchindex (205); -System.out.println ("index position is" +result); in } - to Public voidCreateIndex () { + int[] Type =New int[10000]; - int[] Typenum =New int[10000]; the for(inti = 0; I < This. table.length; i++) { * intn = This. table[i]/m-1; $ if(Type[n] = = 0) {Panax NotoginsengType[n] = 1; - } thetypenum[n]++; + } A intStart = 0; the for(inti = 0; i < typenum.length; i++) { + if(Typenum[i] = = 0) { - Break; $ } $Indexitem item =NewIndexitem (); -Item.index =i; -Item.Start =start; theItem.length =Typenum[i]; - Indextable.add (item);WuyiStart + =Typenum[i]; the } - } Wu - Public voidInsertindex (intkey) { About intn = key/m-1; $ intindex =-1; - //Update Index Table - for(inti = 0; I < This. Indextable.size (); i++) { - if(n = = This. Indextable.get (i). Index) { Aindex = This. Indextable.get (i). Start ++ This. Indextable.get (i). length; the This. Indextable.get (i). length++; - Break; $ } the } the the for(inti = n + 1; I < This. Indextable.size (); i++) { the This. Indextable.get (i). start++; - } in the //updating an array the int[] temp =New int[ This. table.length + 1]; About for(inti = 0; i < temp.length; i++) { the if(I <index) { theTemp[i] = This. Table[i]; the}Else if(i = =index) { +Temp[i] =key; -}Else { theTemp[i] = This. table[i-1];Bayi } the } the - This. Table =temp; - } the the Public intSearchindex (intkey) { the intn = key/m-1; the intStart =-1; - intLength =-1; the //Find index location the for(inti = 0; I < This. Indextable.size (); i++) { the if(n = = This. Indextable.get (i). Index) {94Start = This. Indextable.get (i). Start; theLength = This. Indextable.get (i). length; the Break; the }98 } About - if(Start = =-1) {101 return-1;102 }103 //finding data from an indexed location104 for(inti = start; I < start + length; i++) { the if( This. table[i] = =key) {106 returni;107 }108 }109 return-1; the }111}
Java data structures and algorithms------Index lookups