Vous êtes sur la page 1sur 2

Complexité

September 12, 2023

[1]: def Rech_dicho(L,val):


n=len(L)
d=0
f=n-1
while d<=f:
mid=(d+f)//2
if L[mid]==val:
return True
if val<L[mid]:
f=mid-1
else:
d=mid+1
return False

[6]: def Rech_sequentielle(L,val):


n=len(L)
for i in range(n):
if L[i]==val:
return True
return False

[3]: def Tri_Bulles(L):


n=len(L)
echange=True
l=n-1
while echange:
echange=False
for i in range(0,l):
if L[i]>L[i+1]:
L[i],L[i+1]=L[i+1],L[i]
echange=True
l-=1

[13]: def Tri_Selection(L):


n=len(L)
for i in range(0,n-1):
ind=i

1
for j in range(i+1,n):
if L[j]<L[ind]:
ind=j
if i!=ind:
L[i],L[ind]=L[ind],L[i]

[5]: def Tri_insertion(L):


n=len(L)
for i in range(1,n):
x=L[i]
j=i
while j>0 and L[j-1]>x:
L[j]=L[j-1]
j-=1
L[j]=x

[10]: def Tab_alea(n,a,b):


'''Fonction qui crée et retourne un tableau
de n entiers compris entre a et b'''
import random
L=[]
for i in range(n):
L.append(random.randint(a,b))
return L

[15]: L=Tab_alea(15,1,50)
print("avant le tri:",L)
Tri_insertion(L)
print("après le tri:",L)

avant le tri: [48, 42, 50, 41, 42, 13, 22, 2, 11, 12, 50, 33, 41, 30, 40]
après le tri: [2, 11, 12, 13, 22, 30, 33, 40, 41, 41, 42, 42, 48, 50, 50]

Vous aimerez peut-être aussi