#!/bin/python3

from random import randint
from math   import log
import matplotlib.pyplot as plt
import argument as arg

arg.cmdline()


def trier ( n, k ) :
    lst  = [ randint(0, k)  for i in range(0, n) ]
    cpt  = 0
    for i in range( 1, n ) :
        v = lst[ i ]
        while i > 0 and v < lst[i-1] :
            lst[i] = lst[i-1]
            i = i - 1
            cpt +=1
        lst[i] = v
    return cpt



from random import randint
import time
print( 'r=', arg.repetition, 'i=',arg.iteration , 'p=', arg.parm  )


x = []
y = []
z = []
f = []
p = []

A=1/8

for n in  range(2 , arg.iteration  ) :
	cpt = 0
	for  r in range( arg.repetition  ) :
		tps =  trier( n , arg.parm  - 1 )
		cpt += tps
		if r == 0 :
			pire = favo = tps
		if tps > pire:
			pire = tps
		if tps < favo :
			favo = tps
	x.append( n )
	z.append( A*n*n )
	y.append( cpt / arg.repetition )
	f.append(  favo   )
	p.append(  pire   )


plt.title("complexité du tri par insertion : "+str(arg.parm)+" couleur")
plt.ylabel('comparaison')
plt.xlabel('n')
plt.plot(x, z,  label='A * n * n,   A=' + str(A)  )
plt.plot(x, p,  label='défavorable')
plt.plot(x, y,  label='moyenne')
plt.plot(x, f,  label='favorable' )
plt.legend()
plt.grid(True)
plt.show()
plt.close()


