java - is binary search more relevant while searching for a word in dictionary? -
i have written algorithm searches word in dictionary. searches 1 or 2 letters of specified length of word.
ex search:-
a**
result should be:-
aid, aim,
i have used linear search algorithm find words matches criteria. want know if binary search can used instead of linear search? if can 1 please give me hint
import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class cse221labassignment1 { /** * @param args command line arguments */ public static final string wordlistfilename = "e:\\brac uni\\cse lab\\cse221 lab\\cse221labassignment1\\src\\cse221labassignment1\\wordlist.txt"; public static final string searchlistfilename = "e:\\brac uni\\cse lab\\cse221 lab\\cse221labassignment1\\src\\cse221labassignment1\\searchlist.txt"; public static void main(string[] args) { // todo code application logic here string wordlist, searchlist; wordlist = readfile(wordlistfilename); //reading wordlist searchlist = readfile(searchlistfilename); //reading searchlist string[] w = wordlist.split("\\,"); //spliting wordlist , putting array string[] s = searchlist.split("\\,"); //spliting searchlist , putting array (int c = 0; c < s.length; c++) { //iterating through searchlist array // string [] refinedlist=new string[w.length]; //array containing list of words matches lenght of search word. // int refinedcounter=0; //counter refinedlist array (int = 0; < w.length; i++) { //iterating through wordlist array if (s[c].length() == w[i].length()) { // refinedlist[refinedcounter]=w[i]; // refinedcounter++; if (lettermatch(w[i], s[c])) { system.out.print(w[i]); if (i < w.length - 1) { system.out.print(","); } else { system.out.println(";"); } } } } } } public static string readfile(string filename) { scanner k = null; try { k = new scanner(new file(filename)); } catch (filenotfoundexception ex) { system.out.println(ex); } string rt = k.nextline(); while (k.hasnextline()) { rt = rt + "," + k.nextline(); //words seperated comma } return rt; } public static boolean lettermatch(string m, string s) { char[] letters = m.tochararray(); char[] searchletters = s.tochararray(); boolean match = false; int c = 0; (; c < s.length(); c++) { if (searchletters[c] != '*') { if (searchletters[c] == letters[c]) { match = true; } else { match = false; } } } if (c != s.length()) { return false; } else { return match; } } }
i recommend using alternative data structure of heavy lifting. try radix tree http://en.wikipedia.org/wiki/radix_tree. let complete words traverse tree opposed having linear list searches.
Comments
Post a Comment