forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximo_minimo_dc.py
More file actions
26 lines (18 loc) · 783 Bytes
/
maximo_minimo_dc.py
File metadata and controls
26 lines (18 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Mínimo e Máximo de um array usando divide & conquer
# Bruno Dantas de Paiva - 2021
# https://github.com/DantasB
import random
def min_max_dc(vetor, inicio, fim):
"""Encontra o valor mínimo e máximo em um vetor usando D&C."""
if inicio == fim:
return vetor[inicio], vetor[inicio]
meio = (inicio + fim) // 2
vetor_min1, vetor_max1 = min_max_dc(vetor, inicio, meio)
vetor_min2, vetor_max2 = min_max_dc(vetor, meio + 1, fim)
return min(vetor_min1, vetor_min2), max(vetor_max1, vetor_max2)
if __name__ == "__main__":
vetor = [random.randrange(10, 100) for _ in range(0, 10)]
print(vetor)
minimo, maximo = min_max_dc(vetor, 0, len(vetor) - 1)
print(f"Min DC: {minimo}")
print(f"Max DC: {maximo}")