Answered step by step
Verified Expert Solution
Question
1 Approved Answer
As discussed in class, the quicksort algorithm starts by partitioning the list: a pivot value is selected, and the list is partitioned around that pivot.
As discussed in class, the quicksort algorithm starts by partitioning the list: a pivot value
is selected, and the list is partitioned around that pivot. For this problem we'll assume that
The pivot is always the element at index of the original list ie before any rearranging
of elements is performed
After partitioning, all the elements to the left of the pivot are the pivot, and all the
elements to the right of the pivot are the pivot.
In the following parts, assume that stuff is a list of integers.
a points Consider this naive algorithm for partitioning a list stuff:
Create two new low and high lists. These will be used to store the elements of stuff
that are or the pivot, respectively.
Loop through all elements of stuff besides the pivot. If the element is the pivot,
copy it into low. If the element is the pivot, copy it into high.
Copy the elements of low back into stuff, then the pivot back into stuff, and finally
the elements of high back into stuff.
Within a Python file named
partitioning.py write a function partitionnaivestuff
that implements the above algorithm. The function should return the final nonnegative
index of the pivot, after partitioning is complete. Note that partitionnaive doesn't need
to return the partitioned list because the actions that it performs will affect the original
list argument. Make sure that your function works for a list of any length
b points The partitioning algorithm from the previous part is not very efficient. Creating
the two lists low and high requires extra time as well as memory. A better way is to perform
the partition inplace, which means that no new lists are created. Instead, we just modify
the elements of the original list directly.
Suppose we have a list a containing the elements As
before, we want to use the first element as the pivot. Here's an algorithm to perform
an inplace partition:
Create two indices, L and U Start L from the beginning of the list; start U from the
end.
Move L forward through the list until we find the first element that's the pivot or
until we run out of elements Move backward through the list until we find the first
element that's the pivot or until we run out of elements
Step 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