Suppose you are given a stack of n pancakes of different sizes. You want to sort the pancakes so that smaller pancakes are on top of larger pancakes. The only operation you can perform is a flip—insert a spatula under the top k pancakes, for some integer k between 1 and n, and flip them all over. Describe an algorithm to sort an arbitrary stack of n pancakes using O(n) flips. [Hint: This problem sounds a bit like the "Tower of Hanoi" probem that you may have encountered in other classes. But don’t be fooled! The solution looks very different.]

Answers

Answer 1
Answer:

Answer:

We will use the following approach to solve the problem:

Explanation:

1. We will identify the largest pancake in the given stack of pancakes, then insert the spatula below that pancake and flip the entire stack consisting of that largest identified pancake and all other pancakes on the top of it. Now the largest pancake will be on the top of the stack. Now insert the spatula at the bottom of stack and flip. In this way the largest pancake will become the bottommost pancake.

Now identify the next second largest pancake and repeat above process, keep on relating that until all are sorted.

This requires almost 2n-3 flips in worst case. ( For n pancakes). So the time complexity is O(n)

2n because one flip is required to bring pancake at top, then in 2nd flip it goes to bottom.

-3 because, the stack becomes sorted once the 2nd last pancake comes to the top. So next three steps are never required.

B) we want to find stack of n pancakes, that take omega (n) steps.

For that to happen, in worst case ( 2n-3 )>= cn , taking c=1

2n-3 >= n , => n >=3

So for n greater than or equal to 3 the condition holds.


Related Questions

Which of the following protocols is used to unsure secure transmissions on port 443?A. HTTPSB. TelnetC. SFTPD. SHTTP
The program that you create for this exercise will begin by reading the cost of a meal ordered at a restaurant from the user. Then your program will compute the tax and tip for the meal. Use your local tax rate when computing the amount of tax owing. Compute the tip as 18 percent of the meal amount (without the tax). The output from your program should include the tax amount, the tip amount, and the grand total for the meal including both the tax and the tip. Format the output so that all of the values are displayed using two decimal places.
Most new information systems must communicate with other, existing systems, so the design of the method and details of these communication links must be precisely defined. These are called ____.
Manufactured computers and cell phones are part of which industry? Electronics Chemicals Machinery Metal products
1. Write an expression whose value is the result of converting the str value associated with s to an int value. So if s were associated with "41" then the resulting int would be 41.2. Write an expression whose value is the last character in the str associated with s.3. Given variables first and last, each of which is associated with a str, representing a first and a last name, respectively. Write an expression whose value is a str that is a full name of the form "Last, First". So, if first were associated with "alan" and last with "turing", then your expression would be "Turing,Alan". (Note the capitalization! Note: no spaces!) And if first and last were "Florean" and "fortescue" respectively, then your expression’s value would be "Fortescue,Florean".4. Write an expression whose value is the result of converting the int value associated with x to a str. So if 582 was the int associated with x you would be converting it to the str "582".5. Write an expression whose value is the str consisting of all the characters (starting with the sixth) of the str associated with s.

Devices may be arranged in a _____ topology. A) mesh
B) ring
C) bus
D) all of the above

Answers

Answer:

ring

Explanation:

How many times will the following loop display "Looping again!"? for(int i=0; i<=20; i++) cout << "Looping again!" << endl;

Answers

Answer:

21 times

Explanation:

Following are the description of output:

  • As it is for loop and The loop is started from the i=0 , after that check the condition 0<=20 condition is true .The control moves to the body of loop and it print Looping again! on the console window .After that it increment the value of i=i+1 .
  • Now i=1  1<=20  condition is true .The control moves to the body of loop and it print Looping again! on the console window .After that it increment the value of i=i+1 .
  • i=2 2<=20 condition is true .The control moves to the body of loop and it print Looping again! on the console window .After that it increment the value of i=i+1 .
  • ............soon
  • This process is executed again and again but when the value i=21 the loop condition is false .The loop is terminated .
  • As the loop is starts from the i=0 and executed less then equal to 20 therefore 21 times the loop is executed .

a. Business, scientific, and entertainment systems are inherently complex. It is a well-known fact that systems can be better understood by breaking them down into individual tasks. Share how a programmer would use "top-down design" to understand better how a computer program should be designed. How do hierarchy charts aid the programmer?

Answers

Answer:

Explanation:

Top-down design allows a programmer to take a complex idea, break it down into high-level tasks which can be further broken down into more detailed sub-tasks (Evans, Martin, & Poatsy, 2018). The larger a program gets, the more important it is to have a clear design. It is easier to fix an error during the design phase than the coding phase. If the design is flawed, a programmer can end up having to delete or rewrite large portions of code. Reworking large portions of a design is preferable to rewriting large portions of code. Even small programs can become overwhelming with a number of tasks to perform. Hierarchy Chart (hierarchical diagram) shows the breakdown of a system to its lowest manageable parts. It is a top-down modular design tool, constructed of rectangles (that represents the different modules in a system) and lines that connect them. The lines explain the connection and/or ownership between the different modules among the system just like in an organizational chart. main purpose is to describe the structure and hierarchy of an entity. This entity could be a strategy, event, software program, and so on. The hierarchy chart is suitable in any situation that aims to present the organized structure of a material or an abstract entity.

CAN SOMEONE DO OR LOOK UP THE PIE CHART FOR THIS ANSWER PLS AND I WILL MARK YOU BRAINLIEST BUT PLSSS HELP MEEEE!!!

Answers

Answer:

I highly recommened you use ggl charts.

Explanation:

It's free. ALL YOU NEED IS AN GGL ACCOUNT. You can also choose many types of graph you want.

Implement the logic function ( , , ) (0,4,5) f a b c m =∑ in 4 different ways. You have available 3to-8 decoders with active high (AH) or active low (AL) outputs and OR, AND, NOR and NAND gates with as many inputs as needed. In every case clearly indicate which is the Most Significant bit (MSb) and which is the Least Significant bit (LSb) of the decoder input.

Answers

Answer:

See explaination

Explanation:

Taking a look at the The Logic function, which states that an output action will become TRUE if either one “OR” more events are TRUE, but the order at which they occur is unimportant as it does not affect the final result. For example, A + B = B + A.

Alternatively the Most significant bit which is also known as the alt bit, high bit, meta bit, or senior bit, the most significant bit is the highest bit in binary.

See the attached file for those detailed logic functions designed with relation to the questions asked.

Write a class named Episode. An instance of this episode class will represent a single episode of a TV show, with a title, a season number, and an episode number. For example "Friends, season 1, episode 1", or "The Wire, season 3, episode 5." Your class should have the following constructors and methods: A constructor that accepts three arguments: a String for the title, an int for the season number, and an int for the episode number. A constructor that accepts only a String, for the title, and sets the season and episode numbers to 1. A setter and getter for the title. (never mind the season and episode numbers.) A method named "equals", that accepts an Episode, and returns true if the title, season number and episode number are the same as the receiving object's. A method named "comesBefore". This method should also accept an Episode. It should return true if the episode that receives the invocation comes before the parameter. That is only true if the two objects have the same title, and if the argument comes from a later season, or the same season, but a later episode. Write only the class. Do not write a whole program

Answers

Answer:

See explaination

Explanation:

class Episode{

//Private variables

private String title;

private int seasonNumber;

private int episodeNumber;

//Argumented constructor

public Episode(String title, int seasonNumber, int episodeNumber) {

this.title = title;

this.seasonNumber = seasonNumber;

this.episodeNumber = episodeNumber;

}

//Getter and setters

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public int getSeasonNumber() {

return seasonNumber;

}

public void setSeasonNumber(int seasonNumber) {

this.seasonNumber = seasonNumber;

}

public int getEpisodeNumber() {

return episodeNumber;

}

public void setEpisodeNumber(int episodeNumber) {

this.episodeNumber = episodeNumber;

}

public boolean comesBefore(Episode e){

//Check if titles' match

if(this.title.equals(e.getTitle())){

//Compare season numbers

if(this.seasonNumber<e.seasonNumber){

return true;

}

//Check if season numbers match

else if(this.seasonNumber==e.getSeasonNumber()){

return this.episodeNumber<e.getEpisodeNumber();

}

}

return false;

}

atOverride // replace the at with at symbol

public String toString() {

return title+", season "+seasonNumber+", episode "+episodeNumber;

}

}

class Main{

public static void main(String[] args) {

Episode e = new Episode("Friends",1,1);

System.out.println(e);

Episode e2 = new Episode("The Wire",3,5);

System.out.println(e2);

System.out.println(e.getTitle()+" comes before "+e2.getTitle()+" = "+e.comesBefore(e2));

}

}