Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This code is written in Python. The code is incorrect because there may be multiple elements equal to the one we are partitioning around (x)
This code is written in Python. The code is incorrect because there may be multiple elements equal to the one we are partitioning around (x) and while the counts lt, eq, gt are computed correctly, the x may not necessarily occur where they should. On paper submit as problem 1, an example where this incorrect code may lead to incorrect result. Also, change the code (don't change the function names etc.) to x this. Your modied code should also run the partitioning in linear time. The code currently does nothing when you input a command.
import random def swap (L,i,j): swap list positions i and j" t = L[i] L[i] = L[j] Lj] = t. def partition (L): n = len (L) idx = random.randrange (n) #put L[idx] into oth position swap (1,0, idx) x = L[O] #lt is number of elements less than L[idx), eg is number of elements equal #gt is number of elements greater than L[idx] (lt, eg, gt) = (0,1,0) (i,j) = (0,1) while (jStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started