How to search for an element in circular sorted integer array with Python

1 Answer

0 votes
def searchCircularSortedArray(lst,  element) :
    low = 0
    high = len(lst) - 1
    
    while (low <= high) :
        mid = int((low + high) / 2)
        if (element == lst[mid]) :
            return mid
        if (lst[mid] <= lst[high]) :
            if (element > lst[mid] and element <= lst[high]) :
                low = mid + 1
            else :
                high = mid - 1
        else :
            if (element >= lst[low] and element < lst[mid]) :
                high = mid - 1
            else :
                low = mid + 1
    return -1
    
lst = [6, 9, 10, 13, 2, 3, 5, 6, 8]
element = 5

index = searchCircularSortedArray(lst, element)

if (index != -1) :
    print("index = " + str(index), end ="")
else :
    print("Element not found", end ="")


     
     
     
'''
run:
      
index = 6
 
'''

 



answered Nov 24, 2023 by avibootz
...