Files
cours/recurivite/exercice_MJoannic/dichotomie/recursif/main.py

39 lines
1.4 KiB
Python

from sort_list import sort_list
from typing import Any
def dichotomie(liste: list[Any], element: Any, start: int = 0, end: int = None, bypass_sorting: bool = False) -> bool:
"""Performs a dichotomy search to determine if an element exists in a list or not.
Args:
liste (list[Any]): The list in which to search for the element.
element (Any): The element to search for.
start (int, optional): The starting index of the sublist to search. Defaults to 0.
end (int, optional): The ending index of the sublist to search. Defaults to None.
bypass_sorting (bool, optional): If True, skips sorting the list. Defaults to False.
Returns:
bool: True if the element is found, False otherwise.
"""
if not liste:
return False
assert isinstance(element, type(liste[0])), "Wrong type between liste and element"
if not bypass_sorting:
liste = sort_list(liste)
if end is None:
end = len(liste) - 1
if start > end:
return False
middle = (start + end) // 2
if liste[middle] == element:
return True
elif element < liste[middle]:
return dichotomie(liste, element, start, middle - 1, bypass_sorting=True) # bypass_sorting because it's already done
else:
return dichotomie(liste, element, middle + 1, end, bypass_sorting=True) # bypass_sorting because it's already done