-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsMaj.py
84 lines (69 loc) · 1.51 KB
/
qsMaj.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def swap(table,i,j):
temp=table[j]
table[j]=table[i]
table[i]=temp
def partition(table,debut,fin):
pivot=table[debut]
p=debut
g=fin
e=debut
i=debut + 1
# Passage de comparaison à la REFerence
while i < g:
if table[i] > pivot:
g -= 1
swap(table,i,g)
elif table[i]==pivot:
i += 1
else:
swap(table,i,e)
i += 1
e += 1
return (e, g)
def qsAux(table,debut,fin):
#toBench(2,0)
if (debut < fin):
(e,g) = partition(table,debut,fin)
qsAux(table,debut,e)
qsAux(table,g,fin)
def quickSortCorrected(table):
qsAux(table,0,len(table))
def majCorrected(table):
return majAux(table,0,len(table),len(table) // 2 +1)
def majAux(table,debut,fin,tailleRef):
debug(tailleRef)
(e,g)=partition(table,debut,fin)
if g-e >= tailleRef:
debug("egaux")
return (True,table[e])
elif e >= tailleRef:
debug("petits")
return majAux(table,debut,e,tailleRef)
elif fin-g >= tailleRef:
debug("grands")
return majAux(table,g,fin,tailleRef)
else:
return (False,False)
def majSorted(table):
marching=True
sizeCumul=1
sizeCurrent=1
compare=table[0]
i=0
halfSize=len(table) // 2 +1
while marching or sizeCumul <= halfSize:
if table[sizeCumul] == compare:
debug("equal")
debug(table[sizeCumul])
marching=True
sizeCurrent += 1
if sizeCurrent >= halfSize:
return (True, table[sizeCumul])
break
sizeCumul += 1
elif table[sizeCumul] != compare:
compare = table[sizeCumul]
marching=False
sizeCumul += 1
sizeCurrent = 1
return (False,False)