Algorithm Note (ii) draw a random full order

Source: Internet
Author: User


The algorithm note in the previous chapter does not count as an appropriate start for an algorithm family. This chapter will introduce a method of generating random permutations, and the next chapter begins with a formal start of our sorting algorithm.


In our sort algorithm presenter, we need a random set of data to start the sort, and this chapter produces such a set of random data.


(i) Implementation analysis

We need a random set of data, and the method that seems to be produced is not too difficult. We just need to get a random number from the rand () function to modulo the count (the size of the order), the result as an index to the array, whose value is the number of random numbers that are currently generated, assuming that it already exists, and continues to look until the number of random numbers is generated is the same as the total number.

The biggest problem with this approach is that what we need to produce is a set of full permutations, and when the size of the order is large, the last few numbers are horribly low, and such a method can only be discarded.

The group number is Speaking of the whole arrangement, it seems another way, we can through the standard library of an algorithm, Next_permutation (), in order to get the whole arrangement, then we can first order, Then get n times the next full permutation, n is a random number, it's from 0 to the full array of sorts. Here comes the question again, how does the whole arrangement count? Factorial, too terrible, the total number of this species at a certain scale, not necessarily faster than the above method. Such a method is also pass.

Then there is the method we are going to use today, the Draw method. First of all, the way I look at a magazine is not entirely original.

We can think of the example of the licensing of life, after a round of shuffle cards, it seems that only need to do it again, and there is no way to miss the situation. How does it come true? First randomly draw out a card, take it out of the card heap, and then from the rest of the card heap, and then randomly pulled out a card .... And so on

(ii) Building models

In fact, from the above, the model has been very obvious. We need a card box to release vessel the rest of the cards, need to have a card box, to put out the cards. Draw a card randomly from the rest of the cards until the last card is left.

1. Create two arrays, we are able to use vectors, and we can assign count integers to int pointer by dynamic allocation.

2. Then use the rand () function to extract a number from it and deposit it in an array.

3. The remaining card box will be drawn to this number and the remaining box in the last digit exchange, the next time just extract from the count-1 number, the next repeated 2 operations, until the remaining array of 0.

4. Assume that a pointer is used to do some cleanup work.


(c) Code implementation

This code, temporarily do not write, believe that proficiency in any language of the program apes can follow the above method with O (n) efficiency to produce the full array of random numbers.


Well, from the next blog, we are going to start sorting out, a variety of sorting algorithms, will be shown through our demo program one by one, think of a little bit of excitement.



Algorithm Note (ii) draw a random full order

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.