简体   繁体   English

如何仅使用单词而不使用数字来进行二进制搜索?

[英]How do you use a binary search using only words and not numbers?

I was asked to use binary search to find specific words in a file that we read. 我被要求使用二进制搜索在我们读取的文件中查找特定的单词

The issue I do not understand is how to use a binary search when you are looking for words and not numbers . 我不明白的问题是,在查找单词而不是数字时如何使用二进制搜索。

Binary search operates on sorted inputs . 二进制搜索对分类的输入进行操作 You can define an order also on words, not only on values. 您不仅可以根据值,还可以按单词定义顺序

For example the lexicographical order . 例如字典顺序 In Java this is even implemented as the natural order of String s. 在Java中,这甚至被实现为String自然顺序 So you can do "text1".compareTo("text2") and it returns the order. 因此,您可以执行"text1".compareTo("text2")并返回顺序。


A small illustration of binary search: 二进制搜索的一个小例子:

二进制搜索图

As you see, the only thing to decide in the algorithm is the order between two objects. 如您所见,在算法中唯一要决定的就是两个对象之间的顺序。 For example, from the image, 7 < 14 and 7 > 6 . 例如,从图像中, 7 < 147 > 6 As said, you can also do this for String s. 如前所述,您也可以对String执行此操作。 Indeed for everything for which you define an order . 事实上, 一切为您定义的订单

Actually many classes in Java (more than 150 ) implement a natural order , they are listed under the interface Comparable ( documentation ), they all provide a compareTo method with a meaningful order . 实际上,Java中的许多类(超过150 )实现自然顺序 ,它们在Comparable接口( 文档 )下列出,它们都提供具有有意义顺序compareTo方法。

Think of looking up a word in the dictionary; 考虑在字典中查找一个单词; this is an example of binary search. 这是二进制搜索的示例。

For example, lets look up "eunoia": 例如,让我们查找“ eunoia”:

  1. You flip open the dictionary to approximately the "E" section but maybe you go a little too far and end up in the "M" section. 您将字典翻开到大约“ E”部分,但是可能您走得太远而最终到达“ M”部分。
  2. You flip about half way back and now you're in the "E" section (if you're lucky) 您向后翻转约一半,现在进入“ E”部分(如果幸运的话)
  3. Move onto the next letter in the word and repeat. 移至单词中的下一个字母并重复。

This all works because the dictionary is well-ordered , we all agree that A is the 1st letter, B is the 2nd, etc. Another way to look at it is that the Alphabet is the same thing as the numbers [0 - 25] just with different names. 所有这些都是有效的,因为字典是有序的 ,我们都同意A是第一个字母,B是第二个字母,依此类推。另一种查看方式是字母表与数字[0-25]相同只是用不同的名字。

Binary Search for string Implemented in C. 二进制搜索字符串,在C中实现。

    char *lineptr[MAXLINE]  //Array of char pointers stores the address of string
    int binsrch(char srch[],int low,int high)
    {
       int mid;

       if(high>=low){
          mid=(low+high)/2;
          if(strcmp(srch,lineptr[mid])<0) //compare string stored in srch and lineptr[mid]
                return binsrch(srch,low,mid-1,count);
          else if(strncmp(srch,lineptr[mid],count)>0)
               return binsrch(srch,mid+1,high,count);                                                      
          else 
               return mid; // Found
       }
     return -1;  //Not found
  }                                             

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在通用程序中实现对对象数组的二进制搜索? - How do you implement a binary search for object arrays in a general program? 二进制搜索树,您如何找到最大值? - Binary Search Trees, how do you find maximum? 如何对数组进行二进制搜索,即偶数索引上的数字是升序的,奇数索引上的数字是降序的 - how to do binary search on array which is the numbers on even indexes are ascending and the numbers on odd indexes are descending 您将如何在不使用数组而仅使用字符串的情况下按字母顺序排列单词? - How would you alphabetize words with out using array and only strings? 如何创建实现递归二进制搜索以在数组中搜索n个数字的循环? JAVA - How do i create a loop that implement a recursive binary search to search for n numbers in a array? JAVA 您如何读取“字”具有不同字节长度的二进制数据文件? - How do you read in binary data files where the “words” have different byte lengths? 如何使用二叉搜索树查找前缀匹配? - How do I use a binary search tree to find prefix matches? 如何在二叉树中搜索数字和? - how to search sum of numbers in Binary Tree? 使用MultiFieldQueryParser时如何在Lucene中搜索部分单词? - How do I search partial words in Lucene when using MultiFieldQueryParser? 如何使用Axis2将二进制文件发送到Web服务? - How do you send a binary file to a webservice using Axis2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM