Searching 搜索算法

2018/01/01

Categories: Python algorithms searching Tags: algorithms

Linear Search

Search an Unsorted List

def linearSearch(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i
    return None

Analysis of running time:

More efficient linear search:

def linearSearch2(arr, target):
    last, arr[-1] = arr[-1], target
    i = 0
    while arr[i] != target:
        i += 1
    if i < len(arr)-1 or last == target:
        return i
    else:
        return None

def recursiveLinearSearchHelper(arr, target, start, end):
    if start > end:
        return None
    if arr[start] == target:
        return start
    else:
        return recursiveLinearSearchHelper(arr, target, start+1, end)

def recursiveLinearSearch(arr, target):
    # base case
    if arr == []:
        return None
    return recursiveLinearSearchHelper(arr, target, 0, len(arr) - 1)

Search a Sorted List

def linearSearchSorted(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i
        if arr[i] > target:
            return None
    return None

Binary Search

Python code #1

def binarySearch(arr, target):
    low, high = 0, len(arr)-1
    while low <= high:
        mid = (low + high)//2
        if target == arr[mid]:
            return mid
        elif target < arr[mid]:
            high = mid - 1
        else:
            low = mid + 1
    return None

Python code #2 Bad!

def binarySearch(arr, target):
    if len(arr) == 0:
        return None

    mid = len(arr)//2
    if arr[mid] == target: return mid
    elif arr[mid] > target:
        try:
            return binarySearch(arr[:mid], target)
        except TypeError:
            return None
    else:
        try:
            return binarySearch(arr[(mid+1):], target) + mid + 1
        except TypeError:
            return None  

Python code #3

def binarySearchHelper(arr, target, low, high):
    if high < low:
        return None
    mid = (low + high)//2
    if arr[mid] == target:
        return mid
    elif arr[mid] > target:
        return binarySearchHelper(arr, target, low, mid-1)
    else:
        return binarySearchHelper(arr, target, mid+1, high)

def binarySearch(arr, target):
    n = len(arr)
    if n == 0:
        return None
    else:
        return binarySearchHelper(arr, target, 0, n-1)

Amortized Cost