-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from yashksaini-coder/fix-360
✅ Revamped Searching code snippets seperately from sorting
- Loading branch information
Showing
24 changed files
with
555 additions
and
2,098 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,68 @@ | ||
def binary_search(arr, target, low, high): | ||
import logging | ||
from typing import List | ||
|
||
logging.basicConfig( | ||
level=logging.DEBUG, | ||
format="%(asctime)s - %(levelname)s - %(message)s" | ||
) | ||
|
||
def binary_search(arr: List[int], target: int, low: int, high: int) -> int: | ||
"""Perform a binary search on a sorted array. | ||
Args: | ||
arr (List[int]): Sorted list of elements to search through. | ||
target (int): The element to search for. | ||
low (int): Lower index of the search range. | ||
high (int): Higher index of the search range. | ||
Returns: | ||
int: The index of the target if found, else -1. | ||
""" | ||
logging.debug(f"Binary search called with low={low}, high={high}") | ||
if high >= low: | ||
mid = (high + low) // 2 | ||
logging.debug(f"Checking middle index {mid}") | ||
if arr[mid] == target: | ||
logging.info(f"Target {target} found at index {mid}") | ||
return mid | ||
elif arr[mid] > target: | ||
return binary_search(arr, target, low, mid - 1) | ||
else: | ||
return binary_search(arr, target, mid + 1, high) | ||
else: | ||
return -1 | ||
logging.warning(f"Target {target} not found in the array.") | ||
return -1 | ||
|
||
def exponential_search(arr, target): | ||
""" | ||
Exponential search algorithm. | ||
:param arr: Sorted list of elements to search through. | ||
:param target: The element to search for. | ||
:return: The index of the element if found, else -1. | ||
def exponential_search(arr: List[int], target: int) -> int: | ||
"""Exponential search algorithm. | ||
Args: | ||
arr (List[int]): Sorted list of elements to search through. | ||
target (int): The element to search for. | ||
Returns: | ||
int: The index of the target if found, else -1. | ||
""" | ||
if not arr: # Check for empty array | ||
logging.info("Starting exponential search") | ||
if not arr: | ||
logging.error("Empty array provided for search.") | ||
return -1 | ||
if arr[0] == target: | ||
logging.info("Target found at index 0") | ||
return 0 | ||
n = len(arr) | ||
i = 1 | ||
while i < n and arr[i] <= target: | ||
logging.debug(f"Exponential search checking index {i}") | ||
i *= 2 | ||
result = binary_search(arr, target, i // 2, min(i, n - 1)) | ||
if result != -1: | ||
logging.info(f"Element {target} found at index {result}") | ||
else: | ||
logging.info(f"Element {target} is not present in array") | ||
return result | ||
|
||
return binary_search(arr, target, i // 2, min(i, n - 1)) | ||
|
||
# Driver Code | ||
if __name__ == "__main__": | ||
arr = [2, 3, 4, 10, 40] | ||
target = 10 | ||
result = exponential_search(arr, target) | ||
if result == -1: | ||
print("Element is not present in array") | ||
logging.info("Element is not present in array") | ||
else: | ||
print("Element is present at index", result) | ||
logging.info(f"Element is present at index {result}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.