Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Help There is an error in my code that I can't track down. I am trying to find the smallest value in O(log n)

Python Help

There is an error in my code that I can't track down.

I am trying to find the smallest value in O(log n) time.

List is sorted but it may be rotated

This is what I have so far

g=[3, 4, 5, 6, 7, 8, 1, 2] z=len(g) mid=z//2 mid+=1 fir=0 las=z-1

def a(b): return b-1 while (las-fir) > 2: if g[mid+1] > g[mid]: # midfirst 5 < midnext 6 if g[fir] < g[las]: # first 5 < last 6 # [4,5,6,7] #use first print("seta {},{}, {}".format(fir,las, g[fir:las])) las=mid else: # [8,5,6,7] fir=mid print("setb {},{}, {}".format(fir,las, g[fir:las])) else: # midfirst 6 > midnext 3 if g[fir] < g[las]: #first 3 < last 4 # [1,4,3,2] las=mid-1 print("setc {},{}".format(fir,las)) else: # first 6 < las 4 [5,4,3,1] fir=mid+1 print("setd {},{}, {}".format(fir,las, g[fir:las])) mid = (fir + ((las-fir)//2)) mid+=1 print("this is fir{}, mid{}, las{}".format(fir,mid,las)) d=fir e=las-1 print(g[d:e]) if fir==las: print(fir) elif g[d] < g[e]: print(g[d]) else: print(g[e])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions