Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4 Flipping frogs You have encountered a troupe of n frogs who are each labeled with a number 0 , . . . , n

4 Flipping frogs
You have encountered a troupe of n frogs who are each labeled with a number 0,..., n 1,
where n is a power of 2.
The n frogs dance in a line, and they only know one type of dance move, called Flip(i,j):
for any i and j so that 0<= i < j <= n, Flip(i,j) flips the order of the frogs standing in
positions i , i +1,..., j 1. For example, if the frogs started out like this:
01234567
then after executing Flip(1,5), the frogs would look like this:
04321567
Executing this dance move is pretty complicated: it takes the frogs O(|i j|) seconds to
implement Flip(i,j).
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 =[0,4,3,2,1,5,6,7]). In addition to normal
computation, which can be used to compute indices i and j, your algorithm is allowed to call
Flip(i,j), which will cause the frogs to implement the Flip(i,j) 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.
4.1 Partitioning the frogs (15 pt.)
First, you will design an algorithm that tells the frogs how to perform a dance called Partition(i).
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:
04321765
then after executing Partition(3), the frogs partition themselves around the frog in position
3, whose label is 2. The resulting frogs formation could look like this:
7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions

Question

2. What items are typically included in the job description?

Answered: 1 week ago

Question

an element of formality in the workplace between different levels;

Answered: 1 week ago