Can this sort be done?

Source: Internet
Author: User

A set of encyclopedias are placed in a certain layer of the bookshelves, but they are arranged in disorder. A fool wants to sort the books in sequence, that is to say, he wants 1st and 2nd books from left to right on the shelf ,......, Volume n. He ordered this set of books as follows: he kept retrieving the original book that should be placed on the far left and inserting it into its position. For example, if the volume number of a book is 3, it is located at 5th from the left and on the right of its target location. So a fool can take this book out, insert it to the right of the current 2nd books from the left, and squeeze the books that occupy its position to the far right, whether or not this operation will destroy the books already in place. I noticed that this sorting method may have picked up sesame seeds, lost watermelon, and destroyed a series of well-prepared books for the position of a book. It is a matter of ignorance and foresight. The question is, under which circumstances will such sorting be finalized?YesIn what situations can sorting be stuck in endless loops?


 
 
 
 
 
 
Unexpectedly, this sort seems to be a correct one.Algorithm--AnyThe initial sequence can always make the sequence more orderly. To prove this, we encode the series of 01 strings with N bits as follows: The first bits on the left are 1 if and only when the second volume is in the correct position. Sequences 7, 2, 1, 4, 3, 5, 6, 8, and 9 are encoded as 010100011, because in these 9 books, only volumes 2, 4, 8, and 9 are in the correct position.
If a fool puts the k-th volume in the correct position in an operation, consider all books whose number is smaller than K: If it was originally on the left of the k-th book, this operation does not affect it; if it was originally a left K book or a rightmost book (it indicates that it was not in the correct position ), now it is still not possible to be in the correct position (its position may only remain the same or be more right-aligned ). Therefore, the encoding of the front k-1 is not moving, but the K bit from 0 to 1. This means that any operation will always increase the binary code. It is easy to see that as long as the sequence is not orderly, there will always be objects that can be operated by fools, so the sequence encoding will continue to increase and eventually become 111 .... 11. At this time, all the books should be in the position where they should be, and the entire sequence will be ordered.

 
 
 
Now, another fool is coming. His sorting method is basically the same as that of the previous idiot. The only difference is that he can choose any book that is not in the original position (and put it in the place where it should be ), instead of just choosing the books on the right of the target. Can this sort by dummies ensure that this sort of books will eventually become orderly?

 
 
 
 
 
 
The answer is yes again-this method will eventually make all sequences orderly. To prove this, you only need to note that as long as the sequence is not orderly, the fool can always find a valid operation. Therefore, as long as there is no loop, the sequence will eventually become orderly. We will use the reverse identification method to prove that the actions of fools will not generate a loop in the status.
Suppose there is a loop, and in a certain step, the dummies take out the K volume, and it can be assumed that it is moved to a position on the right. Because this is a loop, it sometimes runs back to the left of the target position. This indicates that the idiot must have taken out a book with a larger number than K, put it to the right of K, and squeezed K to the left. You only need to make K the largest book in the book that is moved to the right in the loop, and the conflict arises.

 
 
We will naturally ask the following question: how efficient is this sort algorithm? In other words, in the worst case, how many steps does a fool sort? In the uyhip puzzles in January this year and February, Michael brand discussed this issue in detail. If you are interested, please take a look:

Http://www.brand.site.co.il/riddles/201001q.html
Http://www.brand.site.co.il/riddles/201002q.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.