• edited by
834 views
1 1 vote
Consider a plate stacked with several disks, each of a different diameter (they could all be, for instance, $\text{dosas}$ or $\text{chapatis}$ of different sizes). We want to sort these disks in decreasing order according to their diameter so that the widest disk is at the bottom of the pile. The only operation available for manipulating the disks is to pick up a stack of them from the top of the pile and invert that stack. (This corresponds to lifting up a stack $\text{dosas}$ or $\text{chapatis}$ between two big spoons and flipping the stack.)

How many steps will your algorithm take in the worst case?

1 Answer

0 0 votes
This might sound too straightforward, but I guess the worst case performance would be O(2n) for 'n' elements.

Assumption : The given stack, only one operation is there, we can pick any arbitrary number of elements from the TOS and then flip them.

For worst case, we may assume that the desired element is not present at the bottom or top. It is present anywhere in the middle.

Consider a situation like this for the widest disk :

  ()                 (    )            ()

  ()                   ()              ()

  ()                   ()              ()

(     )       =>     ()     =>     ()   

  ()                   ()              ()

  ()                   ()            (     )

 (i.)                  (ii.)            (iii.)

Thus, we need two operations in worst case, to bring the current widest disk to bottom. Then, we will call the algorithm for the remaining elements, to make the second widest disk to the bottom.

Thus, in worst case, we need 2n operations for this.
Position:
Show:

Related questions

12 12 votes
3 answers 3 answers
3.2k
3.2k views
go_editor asked May 19, 2016
3,153 views
Consider a plate stacked with several disks, each of a different diameter (they could all be, for instance, $\text{dosas}$ or $\text{chapatis}$ of different sizes). We wa...
1 1 vote
1 1 answer
825
825 views
go_editor asked May 27, 2016
825 views
A finite sequence of bits is represented as a list with values from the set $\{0,1\}$—for example, $[0,1,0], [1,0,1,1], \dots[ \: ]$ denotes the empty list, and $[b]$ is ...
1 1 vote
1 1 answer
991
991 views
go_editor asked May 19, 2016
991 views
Let $G=(V,E)$ be a undirected graph. We say $S \subseteq V$ is a clique if and only if for all $u,\: v \: \in S$, the edge $(u, v)$ is in $E$.Now let $G=(V,E)$ be a graph...
1 1 vote
1 1 answer
1.0k
1.0k views
go_editor asked May 19, 2016
1,041 views
Your team is playing a chess tournament against a visiting team. Your opponents have arrived with a team of M players, numbered $1, 2, \dots , M$. You have $N$ players, n...