Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4 Flipping frogs You have encountered a troupe of n frogs who are each labeled with a number 0 , . . . , n
Flipping frogs
You have encountered a troupe of n frogs who are each labeled with a number n
where n is a power of
The n frogs dance in a line, and they only know one type of dance move, called Flipij:
for any i and j so that i j n Flipij flips the order of the frogs standing in
positions i i j For example, if the frogs started out like this:
then after executing Flip the frogs would look like this:
Executing this dance move is pretty complicated: it takes the frogs Oi j seconds to
implement Flipij
In this problem, you will design and analyze an algorithm to sort the n frogs by their labels,
which uses Flip, the only move the frogs know. However, you dont know the original order
of the frogs until you see them, and so you may also need to spend some extra time in
between the Flip operations computing which indices i and j you want to call Flip on
Below, you will consider algorithms which take as input an array that contains the frogs
initial positions for example, if the frogs were originally organized as the picture above after
the flip then the input array would be A In addition to normal
computation, which can be used to compute indices i and j your algorithm is allowed to call
Flipij which will cause the frogs to implement the Flipij dance. The algorithm
will wait until the frogs are done executing that dance move to continue its computations.
Thus, the total running time of the algorithm includes both the time spent computing, and
the time that the frogs spend dancing.
Partitioning the frogs pt
First, you will design an algorithm that tells the frogs how to perform a dance called Partitioni
For this dance, the frogs will partition themselves around the frog in position i so that all of
the frogs whose labels are smaller than that frogs label will end up to the left, and all the
frogs whose labels are larger will end up to the right. For example, if the situation were this:
then after executing Partition the frogs partition themselves around the frog in position
whose label is The resulting frogs formation could look like this:
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