View the step-by-step solution to:

Question

create function ternary_partition(lst) that partitions an unsorted list 

into three

sections: smaller than pivot, equal to pivot, larger than pivot. 

Input: an unsorted list containing one or more integers. 

Output: a pair of integers, where the first integer is the index of the final 

position of the pivot, and the second integer is the index of the first element 

that is larger than the pivot. The pivot should be the element in the 0th position 

in the original list. The original list must be 

partitioned; however, the list is not returned.


For example


a) Let lst1=[3]. Calling ternary_partition(lst1) returns (0,1), as after calling 

function lst1=[3], thus the pivot section starts at 0 and ends at 1 (exclusive).

b) Let lst2=[3,2,2,5,6,3,1,3]. Calling ternary_partition(lst2) returns (3,6), as after calling 

function lst2==[2,2,1,3,3,3,5,6] (or an approximation of this depending 

on the specifics of the implementation), thus the pivot section starts at 3 and ends at 6 (exclusive).

c) Let lst3=[1,2,3]. Calling ternary_partition(lst3) returns (0,1), as after calling function lst3==[1,2,3] 

(or an approximation of this depending on the specifics of the implementation), thus 

the pivot section starts at 0 and ends at 1 (exclusive).


An implementation that changes the given list to reflect the partitioning (the list is changed 'in place') and 

that has a best and worst-case complexity of O(N).

Top Answer

View the full answer
Capture123.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question