“Python de pesquisa binária” Respostas de código

Python de pesquisa binária iterativa

def binary_search(a, key):
	low = 0
	high = len(a) - 1
	while low < high:
		mid = (low + high) // 2
		if key == a[mid]:
			return True
		elif key < mid:
			high = mid - 1
		else:
			low = mid + 1

	return False
webdevjaz

Python de pesquisa binária

def binary_search(arr, item):
	first = 0
	last = len(arr) - 1
	while(first <= last):
		mid = (first + last) // 2
		if arr[mid] == item :
			return True
		elif item < arr[mid]:
			last = mid - 1
		else:
			first = mid + 1	
	return False
Wrong Whale

Python de pesquisa binária

#the best binary search you'll ever witness

def binary_search(arr, target):
    mid = arr[len(arr)//2]
    if mid == target:
        return True
    if len(arr) == 1 and mid != target:
        return False
    if mid < target:
        return binary_search(arr[len(arr)//2: len(arr)], target)
    else:
        return binary_search(arr[0:len(arr)//2], target)
Busy Bison

Pesquisa binária em Python

#blog.icodes.tech
def binary_search(item,my_list):
    found=False
    first=0
    last=len(my_list)-1
    while first <=last and found==False:
        midpoint=(first+last)//2
        if my_list[midpoint]==item:
            found=True
        else:
            if my_list[midpoint]<item:
                first=midpoint+1
            else:
                last=midpoint-1
    return found
Fancy Fly

Python de pesquisa binária

# This is real binary search
# this algorithm works very good because it is recursive

def binarySearch(arr, min, max, x):
    if max >= min:
        i = int(min + (max - min) / 2) # average
        if arr[i] == x:
            return i
        elif arr[i] < x:
            return binarySearch(arr, i + 1, max, x)
        else:
            return binarySearch(arr, min, i - 1, x)
Solo developer

Python de pesquisa binária

# Iterative Binary Search Function
# It returns index of x in given array arr if present,
# else returns -1
def binary_search(arr, x):
    low = 0
    high = len(arr) - 1
    mid = 0
 
    while low <= high:
 
        mid = (high + low) // 2
 
        # If x is greater, ignore left half
        if arr[mid] < x:
            low = mid + 1
 
        # If x is smaller, ignore right half
        elif arr[mid] > x:
            high = mid - 1
 
        # means x is present at mid
        else:
            return mid
 
    # If we reach here, then the element was not present
    return -1
 
 
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
 
# Function call
result = binary_search(arr, x)
 
if result != -1:
    print("Element is present at index", str(result))
else:
    print("Element is not present in array")
Rich Rattlesnake

Respostas semelhantes a “Python de pesquisa binária”

Perguntas semelhantes a “Python de pesquisa binária”

Mais respostas relacionadas para “Python de pesquisa binária” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código