Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search in an unsorted list
#1
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.
Reply
#2
If it's unsorted, how do you divide in any reasonable way? Why not just do a linear search?
Reply
#3
If elements are not in any order a linear search is most efficient.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,148 Nov-03-2023, 05:35 PM
Last Post: huzzug
  search a list or tuple for a specific type ot class Skaperen 8 1,855 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  Use one list as search key for another list with sublist of list jc4d 4 2,109 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Alpha numeric element list search rhubarbpieguy 1 1,746 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  Make nested system directories based on an unsorted list? koebi 0 1,574 Mar-25-2020, 01:14 PM
Last Post: koebi
  Error in Python3.6:free() Corrupted unsorted chunks error sameer_k 2 3,798 Mar-18-2020, 09:37 AM
Last Post: sameer_k
  search binary file and list all founded keyword offset Pyguys 4 2,701 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  Using Python to search through a list of urls jeremy 4 2,813 Dec-18-2019, 11:52 AM
Last Post: Malt
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 6,583 May-29-2019, 02:27 AM
Last Post: heiner55
  Linear search/searching for a word in a file/list kietrichards 3 3,384 Mar-08-2019, 07:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020