sort.py 887 Bytes
Newer Older
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
1

2 3 4 5 6
from random import randint

n = 42
a = [0] * n

7 8
### On remplit le tableau "a"

9 10 11
for i in range(0, len(a)):
    a[i] = randint(0, 100)

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
12 13
print(a)

14 15
### Et maintenant on le trie

16 17
m = 1
while m < len(a):
18
    #@ invariant 1 <= m <= len(a)
19
    #@ invariant forall i,j. 0 <= i <= j < m -> a[i] <= a[j]
20
    #@ variant   len(a) - m
21 22 23 24
    x = a[m]
    k = m
    while k > 0 and a[k-1] > x:
        #@ invariant 0 <= k <= m
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
25 26 27
        #@ invariant forall i,j. 0 <= i <= j < k               -> a[i] <= a[j]
        #@ invariant forall i,j. 0 <= i      < k <      j <= m -> a[i] <= a[j]
        #@ invariant forall i,j.               k < i <= j <= m -> a[i] <= a[j]
28
        #@ invariant forall   j.               k <      j <= m -> x    <  a[j]
29
        #@ variant   k
30 31 32 33 34 35 36
        a[k] = a[k-1]
        k = k - 1
    a[k] = x
    m = m + 1

#@ assert forall i,j. 0 <= i <= j < len(a) -> a[i] <= a[j]

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
37
print(a)
38