🇫🇷 FranceInformatique NSIPremièreAlgorithmique
🧮CH 07📗 NSI Première · 4h/sem

🧮 Algorithmique

Recherche séquentielle O(n), dichotomique O(log n), tri par sélection O(n²), tri par insertion O(n²), notion de complexité.

🔍 Algorithmes de recherche
Recherche séquentielle — O(n)
Définition
Parcourir la liste élément par élément.
Complexité : O(n) — n comparaisons au pire.

def recherche_seq(lst, valeur):
    for i in range(len(lst)):
        if lst[i] == valeur:
            return i   # indice trouvé
    return -1          # absent

AVANTAGE : fonctionne sur TOUTE liste (même non triée)
INconvénient : lent pour grandes listes
Recherche dichotomique — O(log n)
Définition
Prérequis : liste TRIÉE en ordre croissant.

def recherche_dicho(lst, valeur):
    g, d = 0, len(lst) - 1
    while g <= d:
        m = (g + d) // 2
        if lst[m] == valeur:
            return m         # trouvé
        elif valeur < lst[m]:
            d = m - 1        # chercher à gauche
        else:
            g = m + 1        # chercher à droite
    return -1                # absent

COMPLEXITÉ : O(log₂ n)
N=1000 → 10 comparaisons max
N=10⁶  → 20 comparaisons max

PRINCIPE : couper la liste en 2 à chaque étape
Exercices
EX-A1FacileDérouler la dichotomie

lst = [2, 5, 8, 12, 16, 23, 38]. Chercher 23 par dichotomie. Donner g, d, m à chaque étape.

🤖 Résoudre avec IA
← Précédent
Langages & Python
Suivant →
Projet informatique