Binary search for strings python

I'm relatively new to python[3.3] and I'm just trying to do a binary search through a list of words, and cant figure out how to fix my operand types when it comes down to looping through the indices... I continue to get the TypeError. Cant figure out any way around it

def find[L, target]:
    start = 0
    end = len[L] - 1

    while start  target:
            end = midpoint - 1
        elif midpoint < target:
            start = midpoint + 1
        else:
            return midpoint

I'm calling the function as so:

L = ["Brian", "Meg", "Peter", "Joe", "Stewie", "Lois"]

find[L, "Joe"]

tdelaney

66.1k5 gold badges74 silver badges106 bronze badges

asked Dec 17, 2015 at 5:28

7

Your logic seems fine, except for the input and the bug with incrementing and decrementing midpoint instead of middle.

def find[L, target]:
    start = 0
    end = len[L] - 1

    while start  target:
            end = middle - 1
        elif midpoint < target:
            start = middle + 1
        else:
            return midpoint

L = ["Brian", "Joe", "Lois", "Meg", "Peter", "Stewie"] # Needs to be sorted.

print find[L, "Peter"]

Bhargav Rao

47.3k27 gold badges121 silver badges137 bronze badges

answered Dec 17, 2015 at 5:40

jianweichuahjianweichuah

1,3971 gold badge8 silver badges22 bronze badges

2

def find[L, target]:
    start = 0
    end = len[L] - 1
    while start  target:
            end = middle - 1
        elif midpoint < target:
            start = middle + 1
        else:
            return midpoint

    L = ["Brian", "Joe", "Lois", "Meg", "Peter", "Stewie"]
    L = sorted[L]
    print[find[L, "Lois"]]

As pointed out by others, use middle instead of midpoint

And to optimally use binary search, sort the list first

Vivek Sable

9,5063 gold badges36 silver badges51 bronze badges

answered Dec 17, 2015 at 5:44

Prashant YadavPrashant Yadav

5011 gold badge10 silver badges25 bronze badges

1

def binarySearchOnString[arr, x]:
        l = 0
        r = len[arr] - 1
        while [l 

Javascript

function binarySearch[arr,x]

{

    let l = 0, r = arr.length - 1;

        while [l 0]

                l = m + 1;

            else

                r = m - 1;

        }

        return -1;

}

let arr=["contribute", "geeks", "ide", "practice"];

let x = "ide";

let result = binarySearch[arr, x];

if [result == -1]

    document.write["Element not present
"
];

else

    document.write["Element found at "

                   + "index " + result+"
"
];

Output

Element found at index 2


Can I use binary search on strings?

Binary search is searching technique that works by finding the middle of the array for finding the element. For array of strings also the binary search algorithm will remain the same. But the comparisons that are made will be based on string comparison.

How do you write a binary search in Python?

Implement a Binary Search in Python.
# Iterative Binary Search Function method Python Implementation..
# It returns index of n in given list1 if present,.
# else returns -1..
def binary_search[list1, n]:.
low = 0..
high = len[list1] - 1..
mid = 0..
while low

Chủ Đề