-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusionSort.py
64 lines (53 loc) · 1.28 KB
/
fusionSort.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from qsMaj import swap
from implBench import debug
def fusionSort(table):
return fuFinal(table)
#return fusionAux(table,0,len(table))
#fusionSortAux(table,0,len(table))
def fuFinal(table):
if len(table) > 1:
left=fuFinal(table[:len(table)//2])
right=fuFinal(table[len(table)//2:])
return merge(left,right)
elif len(table) ==1:
return table
else:
return []
def fusionAux(table,debut,fin): # DEPRECATED
if fin-debut > 1:
debug(debut)
debug(fin)
debug(fin//2)
left=table[0:len(table)//2]
right=table[len(table)//2:len(table)]
leftAux=fusionAux(left,0,len(left))
rightAux=fusionAux(right,0,len(right))
debug(left)
debug(right)
return merge(left,right)
#return merge(fusionAux(table,debut,fin // 2),fusionAux(table,fin // 2,fin))
elif fin-debut ==1:
return merge(table,[])
else:
return []
def merge(table1,table2):
if ( len(table1) == 0 and len(table2) == 0 ):
return []
elif len(table1) == 0:
return table2
elif len(table2) == 0:
return table1
else:
i=0
j=0
tableMerged=[]
while i < len(table1) and j < len(table2):
if table1[i] <= table2[j]:
tableMerged.append(table1[i])
i += 1
else:
tableMerged.append(table2[j])
j += 1
tableMerged.extend(table1[i:])
tableMerged.extend(table2[j:])
return tableMerged