|
| 1 | +# Exemplos de funções para achar maior número de um vetor |
| 2 | +# Os 2 métodos são recursivos, porém apenas a MaxDC utiliza divisão e conquista |
| 3 | +# Bruno Dantas de Paiva - 2021 |
| 4 | +# https://github.com/DantasB |
| 5 | + |
| 6 | +import random |
| 7 | +from functools import reduce |
| 8 | + |
| 9 | +# Maximo usando Divisão e Conquista |
| 10 | + |
| 11 | + |
| 12 | +def max_dc(vetor, left, right): |
| 13 | + if left == right: |
| 14 | + return vetor[left] |
| 15 | + |
| 16 | + mid = int((left + right) / 2) |
| 17 | + |
| 18 | + max_1 = max_dc(vetor, left, mid) |
| 19 | + max_2 = max_dc(vetor, mid+1, right) |
| 20 | + |
| 21 | + return max_1 if max_1 > max_2 else max_2 |
| 22 | + |
| 23 | + |
| 24 | +def max_1(vetor, maximo, indice): |
| 25 | + if vetor[indice] > maximo: |
| 26 | + maximo = vetor[indice] |
| 27 | + |
| 28 | + if indice < len(vetor) - 1: |
| 29 | + max_1(vetor, maximo, indice+1) |
| 30 | + else: |
| 31 | + print(f"Max1: {maximo}") |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + vetor = [0 for _ in range(0, 10)] |
| 36 | + |
| 37 | + for i in range(len(vetor)): |
| 38 | + vetor[i] = random.randrange(10, 100) |
| 39 | + |
| 40 | + print(vetor) |
| 41 | + |
| 42 | + max_1(vetor, vetor[0], 1) |
| 43 | + |
| 44 | + print(f"Max DC: {max_dc(vetor, 0, len(vetor)-1)}") |
| 45 | + |
| 46 | + print(f"Max Lambda: {max_dc(vetor, 0, len(vetor)-1)}") |
0 commit comments