标签: binary-search

binary_search不适用于vector <string>

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
    string temp;
    vector<string> encrypt, decrypt;
    int i,n, co=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> temp;
            encrypt.push_back(temp);
    }
    for(i=0;i<n;i++)
    {
        cin >> temp;
        decrypt.push_back(temp);
    }
    for(i=0;i<n;i++)
    {
        temp = encrypt[i];
        if((binary_search(decrypt.begin(), decrypt.end(), temp)) == true) ++co;
    }
    cout << co << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它读取两个相等的字符串列表,并且应该打印出第一个列表中有多少单词也可以在第二个列表中找到,简单.不给我经过考验的结果,我认为问题出在binary_search中.你能告诉我为什么吗 ?

c++ string vector binary-search

2
推荐指数
3
解决办法
3129
查看次数

改进Python比较和存在操作

我是Python的新手,希望在向前推进之前能得到一些建议.我有一组整数,我想检查一个给定的元素是否尽可能快地包含在该组中(速度在这里很重要).

使用Python,我应该看看为这些操作(BST等)定制的自定义数据结构,python技巧,如使用any()包装,还是有任何众所周知的Python/C库是这类事情的标准.我不想在这里重新发明轮子,所以我很想知道在Python中使用它的常用方法.

更多背景,元素都预先插入到组中,之后没有发生,因此插入时间无关紧要.这似乎意味着维护一个已排序的组并执行类似二进制搜索的操作将是最好的方法,但我确信这已经实现得比我实现的更有效,并且可以在Python/C lib中使用.有兴趣听听你们的想法.

谢谢!

c python comparison performance binary-search

2
推荐指数
2
解决办法
112
查看次数

二进制搜索python奇怪的行为

请看这段代码:

def chop(array, search):
        lo = 0
        high = len(array) - 1
        while lo <= high:
                mid = (high + lo) /2
                if array[mid] == search:
                        return 'true'
                elif search > array[mid]:
                        low = mid + 1
                else:
                        high = mid - 1
        return 'false'



if __name__ == '__main__':
        a = [1,2,3,4,5,6,7,8,9,10]
        print chop(a, 3)
Run Code Online (Sandbox Code Playgroud)

我写了这个小脚本,它应该在数组中搜索数字 - 常规二进制搜索.所以我运行脚本,例如当我输入时chop(a, 1)我得到了真实,当我输入时chop(a, 2)我得到了真实,但是当我放入时chop(a, 3)我没有得到答案,只是Python Shell中的空行.

有没有人知道发生了什么?

python binary-search

2
推荐指数
1
解决办法
215
查看次数

Java Arrays binarySearch

好的伙伴在这里是我的简单代码,我在其中构建一个String数组并尝试搜索此数组中的字符串:

String[] arr = new String[5];
arr[0] = "ccc";
arr[1] = "aaa";
arr[2] = "bbb";
arr[3] = "eee";
arr[4] = "ddd";

System.out.println(Arrays.binarySearch(arr,"eee"));
Run Code Online (Sandbox Code Playgroud)

直接来自Java 6 binarySearch文档:"在进行此调用之前必须对数组进行排序.如果未对其进行排序,则结果未定义"!

实际上我运行我的代码几次得到输出总是3这是我的NOT SORTED数组中的eee的位置,但结果似乎没有"未定义",如文档所述.

我错过了什么?

谢谢

java arrays binary-search

2
推荐指数
2
解决办法
2780
查看次数

如果尝试对未排序的数据集进行二进制搜索会发生什么?

如果尝试对未排序的数据集进行二进制搜索会发生什么?

java binary-search

2
推荐指数
1
解决办法
1694
查看次数

modify binary search to find the next bigger item than the key

I want to modify the famous binary search algorithm to return the index of the next bigger item instead of the key being searched.

So we have 4 cases:

  1. the key is smaller than all items, return 0.
  2. the key is bigger than all items, return items.length.
  3. the key is found at index x, return x+1.
  4. the key isn't found, return the index of the next bigger one.

e.g:

data = { 1, 3, 5, 7, 9, 11 };
Run Code Online (Sandbox Code Playgroud)
  • search …

algorithm binary-search

2
推荐指数
2
解决办法
9404
查看次数

为二进制搜索排序对象矢量

我有以下课程:

struct EdgeExtended {
    int neighborNodeId;
    int weight;
    int arrayPointer;
    bool isCrossEdge;

};
Run Code Online (Sandbox Code Playgroud)

我想要一个这样的对象的向量,通过neighborNodeId对它进行排序.然后我想搜索特定的neighborNodeId并通过二进制搜索返回对向量中找到的对象的引用.以前我用过地图,所以就是这样的:

map<int, EdgeExtended> neighbours;
.....

auto it = neighbours.find(dnodeId);
if (it != neighbours.end()) {
    edgeMap = it->second; 
}
Run Code Online (Sandbox Code Playgroud)

代替

map<int, EdgeExtended> neighbours;
Run Code Online (Sandbox Code Playgroud)

我希望有

vector<EdgeExtended> neighbours;
Run Code Online (Sandbox Code Playgroud)

并保留尽可能多的旧代码.

我想测量矢量是否比地图快,因为我正在构建数千个矢量(或地图),每个矢量(地图)相对较小(~10个项目).我不知道如何a)通过neighborNodeId对对象进行排序,以及b)如何使用二进制搜索来搜索类的特定成员(neighborNodeId).抱歉,这个菜鸟问题.我指望你的帮助.

c++ vector map binary-search jquery-ui-sortable

2
推荐指数
1
解决办法
1586
查看次数

如何创建可以二进制搜索的"全局"不可变List <T>列表

问题:当我的节目明星我创建一个List<myClass> myList并用对象填充它.这些对象是使用文件中的日期创建的.之后,我不想更改该列表,也不想再次访问这些文件.但我需要在很多类/方法中访问该List

我提出的解决方案:List<myClass> myList在静态类中创建私有,并使用我的静态类的构造函数填充它,并仅使用返回myList.AsReadOnly()的属性访问它.这样我就无法改变实际的清单.但是AsReadOnly的回归是一个IList,对吧?我在MSDN上检查IList并且没有BinarySearch方法......这会使整个事情变得非常缓慢.

我该如何解决这个问题?复制IList返回正常列表,以便我可以排序和BinarySearch吗?或者可能更改整个"myList.AsReadOnly()"方法?我愿意接受各种建议和方法^^改变整个代码不是问题.

编辑:

  1. TL; DR如何创建一个"全局"列表,可以通过我的程序中的任何方法/类访问,可以使用List.Sort方法进行排序,可以二进制搜索并且不能更改它的内容(在创建之后,既不添加也不删除元素.
  2. 我一回到家就会发布一些代码.

c# list readonly binary-search

2
推荐指数
1
解决办法
510
查看次数

二进制搜索小于或等于搜索值的最接近值

我正在尝试编写一种算法,用于查找最接近的值的索引,该索引小于或等于排序数组中的搜索值.在数组[10,20,30]的示例中,以下搜索值应输出以下索引:

  1. searchValue:9,索引:-1
  2. searchValue:10,索引:0
  3. searchValue:28,索引:1
  4. searchValue:55555,索引:2

我想使用二进制搜索进行对数运行时.我有一个C-esque伪代码的算法,但它有3个基本情况.这3个基本案例是否可以压缩为1以获得更优雅的解决方案?

int function indexOfClosestLesser(array, searchValue, startIndex, endIndex) {
  if (startIndex == endIndex) {
    if (searchValue >= array[startIndex]) {
      return startIndex;
    } else {
      return -1;
    }
  }

  // In the simplistic case of searching for 2 in [0, 2], the midIndex
  // is always 0 due to int truncation. These checks are to avoid recursing
  // infinitely from index 0 to index 1. 
  if (startIndex == endIndex - 1) {
    if (searchValue >= …
Run Code Online (Sandbox Code Playgroud)

algorithm search binary-search

2
推荐指数
2
解决办法
6887
查看次数

二元搜索与三元搜索

时间空间复杂度而言,二元搜索是否优于三元搜索

algorithm search binary-search ternary-search

2
推荐指数
1
解决办法
2469
查看次数