C ++ algorithm analysis of the principle of full arrangement (Baidu Xunlei pen exam) (5)

Source: Internet
Author: User

Why contact the full Arrangement

Full sorting is a hot topic in the test interview, because it is difficult to evaluate Recursive Implementation and non-Recursive Implementation, so as to distinguish the examinee's level. Therefore, Baidu and Xunlei have both taken the test in campus recruitment and the examination of programmers and software designers. Therefore, this article summarizes the full arrangement to help you better learn and understand. You are welcome to point out any supplement to this article.

The principle of full arrangement is to sort the number of groups in a certain order. If there are n groups, the total number is n! . The following uses {1, 2, 3, 4, 5} as an example to describe how to compile a fully-arranged recursive algorithm. Analysis of fully-arranged Algorithms

First, let's take a look at how the question is required (Baidu Xunlei's strokes ).

Write a function in C ++, such as Foo (const char * str), and print out the full arrangement of str,
Such as abc, acb, bca, dac, cab, and CBA.

For convenience, use 123 as an example. 123 is arranged in the ascending order of 123, 132, 213, 231, 312, and 321. First, consider how the number 213 and 321 is obtained. Obviously, both of them are obtained from the exchange between 1 in 123 and the next two numbers. Then, we can exchange the second number of 123 and the third number to get 132. Similarly, 213 and 321 can be obtained based on 231 and 312. So you can know --The full arrangement is to switch each number from the first number to the number next to it.

Based on the above rule and algorithm analysis, we will give the simple code (it seems not very complicated, but I feel very confused. We suggest debugging more, because I also analyzed it like this !)
#include 
 
  using namespace std;int n = 0;  void cout1(int list[]){for (int c = 0; c<=1; c++){printf("%d ", list[c]);  }printf("\n");}void swap(int *a, int *b) {     int m;     m = *a;     *a = *b;     *b = m; }  void perm(int list[], int k, int m)  {     int i;     if(k > m)   //k=0 m=1  ---1  :k =1:  k=2: {          for(i = 0; i <= m; i++)              printf("%d ", list[i]);          printf("\n");         n++;     }     else     {         for(i = k; i <= m; i++)    //k=0,m=1 --1 :k =i =1:{             swap(&list[k], &list[i]);  // cout1(list);perm(list, k + 1, m);   //m =k =1://1swap(&list[k], &list[i]);         }     } } int main() {     int list[] = {1, 2,3 };     perm(list, 0, 2);     printf("total:%d\n", n); system("pause");return 0; }  
 

The running effect is as follows:

OK! Please elaborate on Debug. Are there any new discoveries? What if I change all to the same one? Change to 222!

The running effect is as follows:

Let's digest so much. Next we will remove the recurrence of repeated Full Permutation and use non-recursive implementations of Full Permutation!

(If you have a more efficient algorithm, please share it with us !)

We are looking forward to continuous updates!

Syw_selfimip Sina Weibo address: http://weibo.com/u/2945271402

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.