Skip to content

Instantly share code, notes, and snippets.

@danielnunesdc
Created February 16, 2018 02:26
Show Gist options
  • Select an option

  • Save danielnunesdc/ade08fe712b5de99352a0b2ef82733b2 to your computer and use it in GitHub Desktop.

Select an option

Save danielnunesdc/ade08fe712b5de99352a0b2ef82733b2 to your computer and use it in GitHub Desktop.
from time import time
from random import shuffle
def busca_sequencial_sentinela(v, x):
i = 0
v.append(x)
while v[i] != x:
i += 1
if i == len(v) - 1:
return -1
return i
vetor = list(range(0, 101))
shuffle(vetor)
print(vetor)
chave = 40
antes = time()
posicao = busca_sequencial_sentinela(vetor, chave)
depois = time()
total = (depois - antes) * 1000
if posicao >= 0:
print('\nO elemento {} foi encontrado na posição {}.'.format(chave, posicao))
else:
print('\no elemento NÃO foi encontrado.')
print('\nO tempo total gasto foi {:.2} ms.'.format(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment