Last active
April 23, 2026 00:23
-
-
Save vtmx/f8378fc848b3dce5df8bee05e5832241 to your computer and use it in GitHub Desktop.
pesquisa_binaria.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function pesquisa_binaria(lista, item) | |
| local interacao = 1 | |
| local baixo = 1 | |
| local alto = #lista | |
| while baixo <= alto do | |
| interacao = interacao + 1 | |
| local meio = math.floor(#lista / 2) | |
| local chute = lista[meio] | |
| if chute == item then | |
| return chute, interacao | |
| end | |
| if chute > item then | |
| alto = meio - 1 | |
| else | |
| baixo = meio + 1 | |
| end | |
| end | |
| return nil, interacao | |
| end | |
| local function main() | |
| local lista = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } | |
| local item = 5 | |
| local chute, interacao = pesquisa_binaria(lista, item) | |
| print('Item ' .. chute .. ' encontrado na interação ' .. interacao) | |
| end | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment