1 packagesearch;2 3 Importjava.util.Scanner;4 5 /*typically, The average number of times a keyword is compared during a lookup, also called the average lookup length (ASL), is the criterion for measuring the efficiency of a search algorithm:6 * asl= summation (p[i]*c[i]), (i=1~n). p[i] the probability of finding the first record, c[i] the number of times that I have found the first record to compare*/7 /*the idea of sequential lookups: starting at one end of the table, sequentially scanning the linear table, sequentially comparing the scanned keyword to the given value k, if the currently scanned keyword is equal to k, the lookup succeeds,8 * If no record of the keyword equals k is found after the scan is complete, the lookup fails. 9 * ASL1 Find success (sum) I/n,i=1~n.Ten * ASL2 find failed n*/ one a //the first line enters the sequence to find, the second line enters the keyword - public classShunxusearch { - the public Static voidmain (string[] Args) { -Scanner cin =NewScanner (system.in); -string[] s=NewString[2]; - for(intI =0;i<2;i++){ +s[i] =Cin.nextline (); - } +string[] st = S[0].split (""); a int[] C =New int[st.length]; at for(intj=0;j<st.length;j++){ -c[j]=Integer.parseint (st[j]); - } - intKey = Integer.parseint (s[1]); - intresult =Search (c,key); - System.out.println (result); in Cin.close (); - } to public Static intSearchint[] R,intk) { + inti,n=r.length; - for(i=0;i<n;i++){ the if(r[i]==k) { * returni; $ } Panax Notoginseng } - return-1; the } +}
Java Implementation Order Lookup