-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMergeSort-1.py
More file actions
44 lines (39 loc) · 890 Bytes
/
MergeSort-1.py
File metadata and controls
44 lines (39 loc) · 890 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import random
# DIVIDE A LISTA EM DUAS PARA O MERGE SORT
def separador(lista):
l1 = []
l2 = []
for i in range(0,len(lista)/2):
l1.append(lista[i])
for i in range(len(lista)/2,len(lista)):
l2.append(lista[i])
return l1,l2
# MERGE JUNTA LISTA
def juntalista(lista1,lista2):
novalista = []
while len(lista1) != 0 and len(lista2) != 0:
if lista1[0] >= lista2[0]:
novalista.append(lista2.pop(0))
else:
novalista.append(lista1.pop(0))
if len(lista1) != 0:
for e in lista1:
novalista.append(e)
elif len(lista2) != 0:
for e in lista2:
novalista.append(e)
return novalista
# SORT
def merge(lista):
# CASOS DE PARADA
if len(lista) < 2: return lista
# RECURSIVIDADE
else:
lis1,lis2 = separador(lista)
return juntalista(merge(lis1),merge(lis2))
#Teste
while True:
h = random.sample(range(0,100),100)
if sorted(h) != merge(h):
print h
break