Académique Documents
Professionnel Documents
Culture Documents
Abahchouch Youness
CPGE Agadir
REDA SLAOUI
Département d’informatique
abahchouch@gmail.com
2024
Introduction algorithmes de tri Algorithme de recherche
Introduction
1/18
2/18
Tri à bulle
Tri à bulle
3/18
tri à bulle
4/18
tri à bulle
4/18
tri à bulle
5/18
6/18
7/18
7/18
1 def r e c h e r c h e _ i n d i c e _ m a x (L , i ) :
2 ind_max =0
3 for j in range ( i ) :
4 if L [ j ] > L [ ind_max ]:
5 ind_max = j
6 return ind_max
7
8 def tri_selection ( L ) :
9 n = len ( L )
10 for i in range (n -1 ,0 , -1) :
11 ind_max = r e c h e r c h e _ i n d i c e _ m a x (L , i )
12 if ind_max != i :
13 L [ i ] , L [ ind_max ]= L [ ind_max ] , L [ i ]
8/18
9/18
10/18
10/18
C’est le tri souvent utilisé naturellement pour trier des cartes à jouer :
l’objectif d’une étape est d’insérer le i-ème élément à sa place parmi ceux qui
précèdent. Il faut pour cela trouver où l’élément doit être inséré en le
comparant aux autres, puis décaler les éléments afin de pouvoir effectuer
l’insertion.
1 def tri_insertion ( L ) :
2 n = len ( L )
3 for i in range (1 , n ) :
4 pos = i
5 while L [ pos -1] > L [ i ] and pos >0:
6 pos = pos -1
7 print ( pos )
8 elt = L [ i ]
9 for j in range (i , pos , -1) :
10 L [ j ]= L [j -1]
11 L [ pos ]= elt
11/18
recherche séquentielle
12/18
recherche séquentielle
1 def r e c h e c h e r _ s e q u e n t i e l l e (L , val ) :
2 n = len ( L )
3 for i in range ( n ) :
4 if L [ i ]== val :
5 return True
6 return False
13/18
recherche dichotomique
La recherche dichotomique
14/18
recherche dichotomique
15/18
recherche dichotomique
16/18
recherche dichotomique
17/18
recherche dichotomique
1 def r e c h e r c h e _ d i c h o t o m i q u e (L , val ) :
2 debut , fin =0 , len ( L ) -1
3 milieu =( debut + fin ) //2
4 while debut < fin and L [ milieu ] != val :
5 if L [ milieu ] > val :
6 fin = milieu -1
7 else :
8 debut = milieu +1
9 milieu =( debut + fin ) //2
10 return L [ milieu ] == val
18/18