1 PackageIYou.neugle.search;2 3 Public classHash_search {4 Private Static intm = 13;5 Private Static int[] hash =New int[m];6 Private Static int[] Array =New int[] {13, 25, 2, 60, 39, 52 };7 8 Public Static voidMain (string[] args) {9 Inserthash ();TenSYSTEM.OUT.PRINTLN ("Hash table is established as follows:"); OneSystem.out.print ("["); A for(inti = 0; i < hash.length; i++) { - System.out.print (Hash[i]); - if(I! = hash.length-1) { theSystem.out.print (","); - } - } -System.out.println ("]"); +System.out.println ("39 in hash Table position:" + searchhash (39)); - } + A Public Static voidInserthash () { at //The method of dividing and taking remainder - for(inti = 0; i < Array.Length; i++) { - intValue =Array[i]; - intAddress = value%m; - while(hash[address]! = 0) { -Address = (++address)%m; in } -Hash[address] =value; to } + } - the Public Static intSearchhash (intkey) { * //Open Address Law $ intAddress = key%m;Panax Notoginseng while(hash[address]! = 0 && hash[address]! =key) { -Address = (++address)%m; the } + A if(hash[address] = = 0) { the return-1; + } - returnaddress; $ } $}
Java data structures and algorithms------Hash Lookup