Python Forum

Full Version: Search in an unsorted list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I'm trying to create a function that searches a key in an unsorted list with a divide-and-conquer algorithm.

Right now it only searches on the right side of the list. How do I manage that it continues searching on the left side of the list?

 def divcon(List, begin, end, key):

    if  end > len(List) - 1 or begin > len(List) - 1 or end < 0 or begin < 0:
        return False

m = (begin + end) // 2
    if (L[m] == key):
        return True

    if begin > end:
        return divcon(List, begin, m – 1, key)
    else:
        return divcon(List, m + 1, end, key)
 
I'd be very grateful for any help.
If it's unsorted, how do you divide in any reasonable way? Why not just do a linear search?
If elements are not in any order a linear search is most efficient.