2016-09-18 144 views
0

计划从排序字符串的txt文件读取和使用顺序,反复二进制和递归二进制数组存储然后搜索数组中的位置和多少次迭代花了查找的单词。尝试将数组中的单词与用户输入的单词进行比较时发生错误。不知道为什么。 2)希望有人能解释迭代二进制和递归二进制之间的差异。 3)为什么必须做到这一点...比较字符串数组和字符串二分法查找

SearchString在SI =新SearchString在();

程序下面是...

import ... 

public class SearchString 
{ 
public static final String TO_STOP = "-1"; 
public static final int NOT_FOUND = -1; 
public static final int MAX_SIZE = 15000; 

    public static int count1; 
    public static int count2; 
    public static int count3; 


    public int sequentialSearch(String[] wordsArray, String word2Search) 
    { 
    int low = 0; 
    int high = wordsArray.length - 1; 
    for (int i = low; i <= high; i++) 
    { 
     count1++; 
     if (wordsArray[i] == word2Search) 
      return i; 
    } 
    return NOT_FOUND; 
    } // end of sequentialSearch() 

    public int binarySearch(String[] wordsArray, String word2Search) 
    { 
    int low = 0; 
    int high = wordsArray.length - 1; 
    while (low <= high) 
    { 
     int mid = (low + high)/2; 
     count2++; 
     if (wordsArray[mid] > word2Search){ 
      high = mid - 1; 
     } else if (wordsArray[mid] < word2Search){ 
      low = mid + 1; 
     } else 
      return mid; 
    } 
    return NOT_FOUND; 
    }/


    public int binarySearch2(String[] wordsArray, int low, int high, String word2Search){ 
    if (low > high) 
     return NOT_FOUND; 
    int mid = (low + high)/2; 
    count3++; 
    if (wordsArray[mid] > word2Search){ 
     return binarySearch2(wordsArray, low, mid-1, word2Search); 
    } else if (wordsArray[mid] < word2Search){ 
     return binarySearch2(wordsArray, mid+1, high, word2Search); 
    } else 
     return mid; 
    } 



public static void main(String[] args) throws IOException 
{ 
    Scanner keyboard = new Scanner(System.in); 

    boolean wantToContinue = true; 

    Scanner stringsFile = new Scanner (new File("sortedStrings.txt"));//.useDelimiter(",\\s*"); 
    List<String> words = new ArrayList<String>();   

    String token1 = "";          

    (stringsFile.hasNext())       
    {  token1 = stringsFile.next(); 
      words.add(token1); 
    } 
    stringsFile.close();          
    String[] wordsArray = words.toArray(new String[0]);  

    System.out.println(Arrays.toString(wordsArray)); 

    SearchString si = new SearchString(); 

    do { 
     System.out.print("Type a word to search or -1 to stop: "); 
     String word2Search = keyboard.nextLine(); 
     if (word2Search.equals(TO_STOP)){ 
      wantToContinue = false; 
     } else { 
      count1 = count2 = count3 = 0; 
      int index; 

      index = si.sequentialSearch(wordsArray, word2Search); 
      if (index == NOT_FOUND) 
       System.out.println("sequentialSearch()  : " + word2Search + " is not found (comparison=" + count1 + ")."); 
      else 
       System.out.println("sequentialSearch()  : " + word2Search + " is found in [" + index + "] (comparison=" + count1 + ")."); 

      index = si.binarySearch(wordsArray, word2Search); 
      if (index == NOT_FOUND) 
       System.out.println("iterative binarySearch(): " + word2Search + " is not found (comparison=" + count2 + ")."); 
      else 
       System.out.println("iterative binarySearch(): " + word2Search + " is found in [" + index + "] (comparison=" + count2 + ")."); 

      index = si.binarySearch2(wordsArray, 0, wordsArray.length-1, word2Search); 
      if (index == NOT_FOUND) 
       System.out.println("recursive binarySearch(): " + word2Search + " is not found (comparison=" + count3 + ")."); 
      else 
       System.out.println("recursive binarySearch(): " + word2Search + " is found in [" + index + "] (comparison=" + count3 + ")."); 
     } 
    } while (wantToContinue); 

} 

}

+1

如果你有一个“有序字符串的txt文件”,你为什么需要对它进行排序? –

+1

什么,哪里是错误?正在处理哪些数据导致它? –

+0

在binarySearch方法和binarySearch2方法中。当我比较words.Array [MID]> word2Search 排序是不准确的,编辑question-- 操作员>是未定义的参数类型(一个或多个)java.lang.String中,java.lang.String中 – DRT

回答

0

如何比较String S(或任何其他类型的Comparable对象):

  • a > b成为a.compareTo(b) > 0
  • a < b成为a.compareTo(b) < 0
  • a == b成为a.equals(b)