{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# pp1 - 1 S ORTING There are a great many algorithms for...

This preview shows pages 1–3. Sign up to view the full content.

1. S ORTING There are a great many algorithms for sorting a collection of objects (viewed as a permutation), and their analysis spans a vast amount of computer sci- ence literature. Typically this analysis concerns worst-case performance. On the other hand, study of Permutation Patterns, which is now firmly established as a branch of combinatorics, began in the late 1960s with the consideration of very limited sorting machines which can’t even sort every collection of objects . This too is how we begin these lectures. 1.1. S TACKS Suppose we are given the permutation π of length n , which we think of just as an ordering π 1 , π 2 , . . . , π n of the numbers 1 through n . We are allowed to pass π through a machine known as a stack , which is, as it sounds, a vertical pile which can hold entries of the permutation. There are two operations we may perform at each stage: we may push the next entry of the permutation onto the top of the stack, or we may pop the topmost entry of the stack off the stack into the output. By passing π through this stack once, can we sort it into the identity permutation 12 n ? For example, Figure 1.1 shows the sorting of the permutation π 2143 . output 2143 input output 2 143 input output 2 1 43 input Step 1: initialize Step 2: push 2 Step 3: push 1 1 output 2 43 input 12 output 43 input 12 output 4 3 input Step 4: pop 1 Step 5: pop 2 Step 6: push 4 12 output 4 3 input 123 output 4 input 1234 output input Step 7: push 3 Step 8: pop 3 Step 9: pop 4 Figure 1.1: The permutation 2143 can be sorted to the identity 1234 with a single pass through a stack. Can every permutation be sorted with a stack? We invite the reader to attempt to sort 231 before reading on. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 L ECTURE 1 S ORTING Proposition 1.1. The permutation 231 cannot be sorted with a stack. Proof. Any sorting of 231 would have to begin by pushing 2 onto the stack. For the next operation, 2 cannot be popped off the stack, because it must come after 1 , so the only option is to push 3 onto the stack on top of 2 , but then 3 will precede 2 in the output. Therefore there is no way to sort 231 with a stack. The ideas of this proof, extended just a bit, show that there is a canonical stack sorting algorithm. At any point in the stack sorting process, if the next entry in the input is less than the entry on top of the stack, then we should push this entry onto the top of the stack. (In fact, we have no choice in this matter, because the entry on top of the stack is not the next entry to output, so we cannot pop this entry.) Otherwise, if the next entry in the input is greater than the entry on top of the stack, then we cannot push this entry onto the stack, so we have no choice but to pop the top entry of the stack into the output. This operation is allowed so long as the entry on top of
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}