Algorithm that produces the next permutation number

Source: Internet
Author: User
Tags character set int size printf sort

Full sort:

Take M (m≤n) elements from n different elements and arrange them in a certain order, called an arrangement of taking m elements from n different elements. All permutations are called when m=n. For example n=3, the whole sort is: 123, 132, 213, 231, 312, 321 altogether 6 kinds.

Dictionary order Method:

A sequence of characters in a given character set is specified, on the basis of which two permutations are specified: From left to right, the corresponding character size is compared. Character set {1,2,3}, the smaller number is the first, so that the entire order is generated in dictionary order: 123, 132, 213, 231, 312, 321.

1. Now assume that a string of numbers in the full order of input requires the next permutation number that corresponds to the full order of the dictionary order. For example: input 123 output 132, input 12435 output 12453.

Algorithm idea:

1. From the right of the series to the left to find continuous increment sequence, for example: 1,3,5,4,2, where 5-3-2 is an ascending sequence.

2. From the above sequence, find a minimum number (4) larger than the number (3) in front of it, and exchange the two numbers. So 1,3,5,4,2->1,4,5,3,2, this time after the exchange is still an incremental sequence.

3. The new ascending sequence is in reverse order, namely: 1,4,5,3,2 => 1,4,2,3,5

#include <iostream>
using namespace std;
    
void Swap (int * A, int * b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
    
Produces the next permutation number/
/existence returns 1
//Does not exist returns 0
int next (int a[], int n)
{
    int i,j,k;
    Look for a non-descending sequence from right to left, for example, for sequence 1,3,5,4,2, the position of number 5 will be found for
    (k = n-1 k > 0 && a[k-1] >= a[k]; k--);
    if (k = = 0) return 0; For example: For 5,4,3,2,1 case, his next nonexistent, returns 0
    //from the non-descending sequence Li to look for the smallest number (3) larger than the preceding one, that is, the number 4 for
    (i = n-1; A[i] <= a[k-1]; i--); 
  //Exchange 3 and 4
    swap (&a[k-1], &a[i]);
    Reverse the new non-descending sequence
    i = k;
    j = n-1;
    while (I < j) Swap (&a[i++], &a[j--]);
    return 1;
}
    
void pnt (int a[], int n)
{
    int i;
    for (i = 0; i < n; ++i) printf ("%d", A[i]);
    printf ("\ n");
}
    
int main ()
{
    int a[] = {1,2,3,4};
    int size = sizeof (a)/sizeof (a[0]);
    PNT (A, size);
    while (next (a, size))
        PNT (A, size);
    return 0;
}

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.