n个互异元素的全排列

/*
 * =====================================================================================
 *
 *       Filename:  basedonleftrotate.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  09/03/2012 09:54:25 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (erliang),
 *   Organization:  
 *
 * =====================================================================================
 */
/*
 * n个互异元素的全排列
 * 此解法摘抄至网络luxuery-zh.iteye.com/blog/1669841
 * 简单描述:
 *     根本思想是:n个互异元素的全排列,n个元素分别放在第一个位置剩下的n-1个元素全排列的排列的并(这里他们之间并没有交集)
 *     固定第一个元素将其他元素全排列,然后左旋一位,再固定第一个元素(原第二个元素)其他元素全排列,如此往复直到所有元素都移动到了第一个元素位置一次。
 * */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define swap(a,b) {(a)=(a)^(b);(b)=(a)^(b);(a)=(a)^(b);}
static int *leftRotate(int *pa,int len);
/*
 * func describe:array left rotate one place
 * in:
 *     pa:point to zhe first element of an array
 *     len:the length of the array
 * out:
 *     the array
 * checked error:
 *     pa==null;
 * non checked error:
 *     out of the array
 * exception:
 *     out of the array
 * */
static void quanPaiLie(int *pa,int len);
/*
 * func describe:
 *     li yong zuo xuan quan pai lie n ge yuan su
 * in:
 * out:
 *     if there is a return value
 * checked error:
 *     pa==null;
 * non checked error:
 *     
 * exception:
 *     array out of range
 * */
static void printIntArray(int *start,int *end);
/*
 * func describe:
 *     how to get the start pointer of the array,that is the question.a global value?a stati
 *     c value? a inner value?
 * in:
 *     start:the start pointer value of the array
 *     end:the end opinter+1value of the array
 * */
int main(int argc,char **argv){
    int array[]={1,2,3,4};
    printIntArray(array,NULL);
    quanPaiLie(array,sizeof(array)/sizeof(array[0]));
    printIntArray(NULL,array+4);
    return 0;
}
static int *leftRotate(int *pa,int len){
    int tmp;
    int i;
    assert(pa!=NULL);
    assert(len>0);
    tmp=pa[0];
    for (i=0;i<len-1;++i){
        pa[i]=pa[i+1];
    }
    pa[len-1]=tmp;
    return pa;
}
static void quanPaiLie(int *pa,int len){
    int i;
    assert(pa!=NULL);
    assert(len>0);
    //printIntArray(pa,NULL);
    if (len==1){
        //tu be insert
        printIntArray(NULL,pa+1);
        return ;
    }
    for (i=0;i<len;++i){
        quanPaiLie(pa+1,len-1);
        leftRotate(pa,len);
    }
    return ;
}
static void printIntArray(int *start,int *end){
    static int *staticStart;
    int *p;
    if(start==NULL){
        assert(staticStart);
        assert(end);
        for(p=staticStart;p!=end;++p){
            printf("%d ",*p);
        }
        printf("\n");
    }else if(start!=NULL){
        staticStart=start;
    }
}
# undef swap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值