Python Forum
Search in an unsorted list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Search in an unsorted list (/thread-27377.html)



Search in an unsorted list - amir_0402 - Jun-04-2020

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.


RE: Search in an unsorted list - micseydel - Jun-04-2020

If it's unsorted, how do you divide in any reasonable way? Why not just do a linear search?


RE: Search in an unsorted list - deanhystad - Jun-04-2020

If elements are not in any order a linear search is most efficient.