“Implemente una búsqueda binaria de una matriz ordenada de enteros utilizando el código de pseudo.” Código de respuesta

búsqueda binaria iterativa python

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

Implemente una búsqueda binaria de una matriz ordenada de enteros utilizando el código de pseudo.

# Here's the pseudocode for binary search, modified for searching in an array. The inputs are the array, which we call array; the number n of elements in array; and target, the number being searched for. The output is the index in array of target:

    1.Let min = 0 and max = n-1.
    2. Compute guess as the average of max and min, rounded down (so that it is an integer).
    3. If array[guess] equals target, then stop. You found it! Return guess.
    4. If the guess was too low, that is, array[guess] < target, then set min = guess + 1.
    5. Otherwise, the guess was too high. Set max = guess - 1.
    6. Go back to step 2.
Handsome Hamster

Respuestas similares a “Implemente una búsqueda binaria de una matriz ordenada de enteros utilizando el código de pseudo.”

Preguntas similares a “Implemente una búsqueda binaria de una matriz ordenada de enteros utilizando el código de pseudo.”

Más respuestas relacionadas con “Implemente una búsqueda binaria de una matriz ordenada de enteros utilizando el código de pseudo.” en Python

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código