我喜欢一个非常简单的Python程序来实现二进制搜索.
tup=input("enter tup:")
start=0
length=len[tup]
end=tup[length-1]
mid=(int(start)+int(end))/2
key=input("enter value to search")
def search(start,end,key):
if key==tup[mid]
print mid
else if key<tup[mid]
search(start,mid,key)
else if key>tup[mid]
search(mid,end,key)
else
return(-1)
Run Code Online (Sandbox Code Playgroud)
我得到一个错误
File "binsearch.py", line 8
if key==tup[mid]
^
Run Code Online (Sandbox Code Playgroud)
SyntaxError:语法无效
我相信我错过了一些微不足道但无法弄清楚的东西.如果您觉得还有其他错误,请告诉我.谢谢 :)
为什么Java中存在非泛型二进制搜索版本?
是因为他们不想让现有的实施崩溃吗?
当找不到结果时,我得到一个奇怪的输出.
import java.util.Arrays;
import java.util.Comparator;
public class BinarySearch {
public static void main(String args[]) {
String arr[] = { "c", "a", "e", "f", "z" };
MySort ms = new MySort();
Arrays.sort(arr, ms);
for (String c : arr) {
System.out.println(c);
}
System.out.println(Arrays.binarySearch(arr, "b", ms));
}
static class MySort implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
zfeca-6
-2当我将"y"作为查询参数-5传递给我时,为什么会打印出来b.如果找不到结果,任何人都可以让我知道发生了什么.
大家好,几周前开始用 C 编程,学习算法,只是想知道如何让我的代码更简单,它只是一个二进制搜索函数。但唯一的事情是你必须保持论点相同,提前致谢。
bool search(int value, int values[], int n)
{
int min = values[0];
int max = values[n-1];
int average = (min + max) / 2;
if(average == value)
{
return true;
}
while (average > value)
{
max = average - 1;
average = (min + max) / 2;
}
while (average < value)
{
min = average + 1;
average = (min + max) / 2;
}
if (max < min)
{
return false;
}
if (average …Run Code Online (Sandbox Code Playgroud) 我正在对数组进行二进制搜索.但有些事情要关闭.它返回-1,好像没有找到目标但是它在数组中.例如:当我将键放在555时,它返回正确的索引但是当我尝试使用下面的例子时,它返回-1 ..
public class bSearch {
public static void main(String[] args) {
int[] nums = {9,5,2,5,7,8,3,22,555};
int key = 8;
System.out.println(searchForNum(nums,0,nums.length-1,key));
}
private static int searchForNum(int[] arr,int first, int last, final int target) {
int middle= (first+last)/2;
if(last < first) {
return -1;
}
if(arr[middle]== target)
return middle;
else if(target < arr[middle]) {
return searchForNum(arr,first,middle-1,target);
} else {
return searchForNum(arr,middle+1,last,target);
}
}
}
Run Code Online (Sandbox Code Playgroud) 嗨,我从以下JavaScript代码中获取未定义.我想要一个调试JavaScript的工具,Webstorm是最好的吗?
//input
var inputArray = [1, 2, 3, 4, 5, 5, 5, 6, 66];
var searchValue = 2;
//output
var arrayLength = inputArray.length;
var arrayCurrent = inputArray;
var currentIndex = arrayLength;
function binarySearch() {
currentIndex = Math.floor(arrayCurrent.length / 2);
if (searchValue == arrayCurrent[currentIndex]) {
var x=currentIndex;
return x;
} else if (searchValue > arrayCurrent[currentIndex]) {
arrayCurrent = arrayCurrent.slice(currentIndex + 1);
binarySearch();//recursive call
} else if (searchValue < arrayCurrent[currentIndex]) {
arrayCurrent = arrayCurrent.slice(0, currentIndex - 1);
binarySearch();//recursive call
}
}
var …Run Code Online (Sandbox Code Playgroud) 为二分查找创建递归函数.
此函数接受已排序的数组和要搜索的项,并返回项的索引(如果项在数组中),或返回-1(如果项不在数组中).
此外,编写测试程序来测试您的功能.
template <class elemType>
int orderedArrayListType<elemType>::binarysearch
(const elemType& item) const
{
int first= 0;
int last = length -1;
int mid;
int list[];
int BinarySearch(,Type & Item, int first, int last)
bool found = false;
while (first <= last && !found){
mid = (first + last) / 2;
if (list[mid] > item)
return BinarySearch(list, item, first, mid -1)
found = true;
else if (list[mid] > item)
return BinarySearch( list, item, first, mid -1)
last = mid - 1; …Run Code Online (Sandbox Code Playgroud)