-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgos_tri.py
More file actions
66 lines (59 loc) · 1.33 KB
/
algos_tri.py
File metadata and controls
66 lines (59 loc) · 1.33 KB
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
65
66
def tri_selection(t):
n = len(t)
for i in range(n-1, 0, -1):
j = indice_max(t,i)
t[j], t[i] = t[i], t[j]
def indice_max(t,i):
j = 0
for k in range(0, i+1):
if t[k] > t[j]:
j = k
return j
def tri_insertion(t):
n = len(t)
for i in range(1,n):
x = t[i]
k = i
while k >= 1 and t[k-1] > x:
t[k] = t[k-1]
k -= 1
t[k] = x
def partition(t,i,j):
pivot = t[i]
k = i + 1
for l in range(i+1, j+1):
if t[l] < pivot:
t[l], t[k] = t[k], t[l]
k += 1
t[i], t[k-1] = t[k-1], t[i]
return k-1
def tri_rapide(t,i,j):
if i < j:
m = partition(t,i,j)
tri_rapide(t,i,m-1)
tri_rapide(t,m+1, j)
def fusion(t1, t2):
u = [0]*(len(t1) + len(t2))
i = j = 0
while i < len(t1) and j < len(t2):
if t1[i] <= t2[j]:
u[i+j] = t1[i]
i += 1
else:
u[i+j] = t2[j]
j += 1
if i == len(t1):
for k in range(j, len(t2)):
u[i+k] = t2[k]
else:
for k in range(i, len(t1)):
u[j+k] = t1[k]
return u
def tri_fusion(t):
if len(t) <= 1:
return t
else:
m = len(t)//2
t1 = tri_fusion(t[:m])
t2 = tri_fusion(t[m:])
return fusion(t1, t2)