{"record":{"id":"4dc010f1cb406f9f","repo":"TheAlgorithms/Python","slug":"sorted-collection-must-be-sorted-in-ascending-orde-4dc010","errorCode":null,"errorMessage":"sorted_collection must be sorted in ascending order","messagePattern":"sorted_collection must be sorted in ascending order","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"searches/exponential_search.py","lineNumber":46,"sourceCode":"    :param item: item value to search\n    :param left: starting index for the search\n    :param right: ending index for the search\n    :return: index of the found item or -1 if the item is not found\n\n    Examples:\n    >>> binary_search_by_recursion([0, 5, 7, 10, 15], 0, 0, 4)\n    0\n    >>> binary_search_by_recursion([0, 5, 7, 10, 15], 15, 0, 4)\n    4\n    >>> binary_search_by_recursion([0, 5, 7, 10, 15], 5, 0, 4)\n    1\n    >>> binary_search_by_recursion([0, 5, 7, 10, 15], 6, 0, 4)\n    -1\n    \"\"\"\n    if right < 0:\n        right = len(sorted_collection) - 1\n    if list(sorted_collection) != sorted(sorted_collection):\n        raise ValueError(\"sorted_collection must be sorted in ascending order\")\n    if right < left:\n        return -1\n\n    midpoint = left + (right - left) // 2\n\n    if sorted_collection[midpoint] == item:\n        return midpoint\n    elif sorted_collection[midpoint] > item:\n        return binary_search_by_recursion(sorted_collection, item, left, midpoint - 1)\n    else:\n        return binary_search_by_recursion(sorted_collection, item, midpoint + 1, right)\n\n\ndef exponential_search(sorted_collection: list[int], item: int) -> int:\n    \"\"\"\n    Pure implementation of an exponential search algorithm in Python.\n    For more information, refer to:\n    https://en.wikipedia.org/wiki/Exponential_search","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/searches/exponential_search.py#L28-L64","documentation":"Raised by the module-local copy of binary_search_by_recursion inside searches/exponential_search.py when sorted_collection is not in ascending order. exponential_search.py vendors its own recursive binary search rather than importing from binary_search.py, so the same 'sorted_collection must be sorted in ascending order' ValueError exists here as an independent raise site. The guard compares list(sorted_collection) to sorted(sorted_collection) on every recursive call, and right < 0 is normalized to len - 1 before the check.","triggerScenarios":"Calling binary_search_by_recursion from exponential_search with unsorted data, e.g. exponential_search([5, 4, 3], 4) fails at this line; direct calls like binary_search_by_recursion([2, 1], 1, 0, 1).","commonSituations":"Refactoring between binary_search.py and exponential_search.py and assuming they share one implementation (they do not); linting/duplication tools flagging the two copies; fixing the guard in one file but not the other.","solutions":["Sort the collection before calling: exponential_search(sorted(data), item).","If you are maintaining this code, consider having exponential_search.py import binary_search_by_recursion from searches.binary_search to remove the duplicated raise site.","Validate sortedness once at the top level instead of relying on the per-recursion check."],"exampleFix":"# before\ni = exponential_search(data, 7)  # data = [9, 3, 7]\n\n# after\ndata = sorted(data)\ni = exponential_search(data, 7)","handlingStrategy":"validation","validationCode":"data = sorted(data)\nidx = exponential_search(data, item)  # module validates before recursing","typeGuard":"def is_ascending(lst: list) -> bool:\n    return all(a <= b for a, b in zip(lst, lst[1:]))","tryCatchPattern":"try:\n    idx = binary_search_by_recursion(data, item, 0, len(data) - 1)\nexcept ValueError:\n    data = sorted(data)\n    idx = binary_search_by_recursion(data, item, 0, len(data) - 1)","preventionTips":["This file duplicates binary_search.py's function; fixes must be applied to both copies.","Validate once in your own code instead of relying on the per-recursion check.","When refactoring, prefer importing the shared implementation over maintaining two copies."],"tags":["search","exponential-search","duplicated-code","precondition","sorted-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}